Click here to Skip to main content
15,867,835 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: fast directory infos for a lot of files Pin
pitwi6-Dec-22 6:56
pitwi6-Dec-22 6:56 
QuestionCustom Control Styling Pin
Kevin Marois2-Dec-22 8:58
professionalKevin Marois2-Dec-22 8:58 
AnswerRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 0:27
mveRichard Deeming5-Dec-22 0:27 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 12:15
professionalKevin Marois5-Dec-22 12:15 
GeneralRe: Custom Control Styling Pin
Richard Deeming5-Dec-22 21:57
mveRichard Deeming5-Dec-22 21:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 13:57
professionalKevin Marois5-Dec-22 13:57 
GeneralRe: Custom Control Styling Pin
Kevin Marois5-Dec-22 14:10
professionalKevin Marois5-Dec-22 14:10 
QuestionWPF .Net Core Relay Command with Parameters Pin
Kevin Marois1-Dec-22 13:50
professionalKevin Marois1-Dec-22 13:50 
Following this SO article, I'm trying to create a relay command that takes parameters:
<ctrls:MaroisHyperlink LinkText="Forgot Password?"
                        Foreground="SteelBlue"
                        Margin="30,0,0,0"
                        Height="20">

    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LinkClicked">
            <i:InvokeCommandAction Command="{Binding ForgotPasswordClickedCommand}"
                                    CommandParameter="True"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>

</ctrls:MaroisHyperlink>
Here's my RelayCommand<t> class:
namespace Marois.Framework.Core.Utilities
{
    public class RelayCommand<T> : ICommand
    {
        private readonly Action<T> execute;

        private readonly Func<T, bool> canExecute;

        public RelayCommand(Action<T> execute)
            : this(execute, null)
        {
        }

        public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
        {
            ArgumentNullException.ThrowIfNull(execute);

            this.execute = execute;
            this.canExecute = canExecute;
            this.RaiseCanExecuteChangedAction = RaiseCanExecuteChanged;

            SimpleCommandManager.AddRaiseCanExecuteChangedAction(ref RaiseCanExecuteChangedAction);
        }

        ~RelayCommand()
        {
            RemoveCommand();
        }

        public void RemoveCommand()
        {
            SimpleCommandManager.RemoveRaiseCanExecuteChangedAction(RaiseCanExecuteChangedAction);
        }

        bool ICommand.CanExecute(object? parameter)
        {
            return canExecute((T)parameter);
        }

        public void Execute(object? parameter)
        {
            if (CanExecute(parameter))
            {
                execute((T)parameter);
            }
        }

        public bool CanExecute(object? parameter)
        {
            return canExecute == null ? true : canExecute((T)parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, new EventArgs());
            }
        }

        private readonly Action RaiseCanExecuteChangedAction;

        public event EventHandler CanExecuteChanged;
    }

    public static class SimpleCommandManager
    {
        private static List<Action> _raiseCanExecuteChangedActions = new List<Action>();

        public static void AddRaiseCanExecuteChangedAction(ref Action raiseCanExecuteChangedAction)
        {
            _raiseCanExecuteChangedActions.Add(raiseCanExecuteChangedAction);
        }

        public static void RemoveRaiseCanExecuteChangedAction(Action raiseCanExecuteChangedAction)
        {
            _raiseCanExecuteChangedActions.Remove(raiseCanExecuteChangedAction);
        }

        public static void AssignOnPropertyChanged(ref PropertyChangedEventHandler propertyEventHandler)
        {
            propertyEventHandler += OnPropertyChanged;
        }

        private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            // this if clause is to prevent an infinity loop
            if (e.PropertyName != "CanExecute")
            {
                RefreshCommandStates();
            }
        }

        public static void RefreshCommandStates()
        {
            for (var i = 0; i < _raiseCanExecuteChangedActions.Count; i++)
            {
                var raiseCanExecuteChangedAction = _raiseCanExecuteChangedActions[i];
                if (raiseCanExecuteChangedAction != null)
                {
                    raiseCanExecuteChangedAction.Invoke();
                }
            }
        }
    }

}
and here's the VM
private ICommand? _ForgotPasswordClickedCommand;
public ICommand ForgotPasswordClickedCommand
{
    get
    {
        if (_ForgotPasswordClickedCommand == null)
            _ForgotPasswordClickedCommand = new RelayCommand<bool>(p => ForgotPasswordLinkClickedExecuted(p), a => ForgotPasswordCanExecute(a));
        return _ForgotPasswordClickedCommand;
    }
}

private bool ForgotPasswordCanExecute(bool args)
{
    return true;
}

private void ForgotPasswordLinkClickedExecuted(bool args)
{
}
When I set the parameter type to Object then it works fine. But if I set it to Bool, as above, then I get the following exception:
'Unable to cast object of type 'System.String' to type 'System.Boolean'.'

I have an implementation of this a WPF .Net Framework and it works fine. But in the .Net Core app I get the error.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

QuestionForgot Password Pin
Kevin Marois1-Dec-22 13:06
professionalKevin Marois1-Dec-22 13:06 
AnswerRe: Forgot Password Pin
Richard Deeming1-Dec-22 22:10
mveRichard Deeming1-Dec-22 22:10 
QuestionWPF Core Hyperlkink Custom Control Pin
Kevin Marois29-Nov-22 16:29
professionalKevin Marois29-Nov-22 16:29 
AnswerRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming29-Nov-22 21:59
mveRichard Deeming29-Nov-22 21:59 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Kevin Marois30-Nov-22 5:46
professionalKevin Marois30-Nov-22 5:46 
GeneralRe: WPF Core Hyperlkink Custom Control Pin
Richard Deeming30-Nov-22 21:25
mveRichard Deeming30-Nov-22 21:25 
QuestionPath Images Pin
Kevin Marois29-Nov-22 14:57
professionalKevin Marois29-Nov-22 14:57 
AnswerRe: Path Images Pin
Richard Deeming29-Nov-22 23:46
mveRichard Deeming29-Nov-22 23:46 
QuestionDropShadowEffect Above & Below Pin
Kevin Marois29-Nov-22 14:30
professionalKevin Marois29-Nov-22 14:30 
AnswerRe: DropShadowEffect Above & Below Pin
Richard Deeming29-Nov-22 23:43
mveRichard Deeming29-Nov-22 23:43 
GeneralRe: DropShadowEffect Above & Below Pin
Kevin Marois30-Nov-22 5:32
professionalKevin Marois30-Nov-22 5:32 
GeneralRe: DropShadowEffect Above & Below Pin
Richard Deeming30-Nov-22 21:30
mveRichard Deeming30-Nov-22 21:30 
QuestionWhat's Wrong With This Style? Pin
Kevin Marois21-Nov-22 9:57
professionalKevin Marois21-Nov-22 9:57 
AnswerRe: What's Wrong With This Style? Pin
Gerry Schmitz21-Nov-22 10:17
mveGerry Schmitz21-Nov-22 10:17 
GeneralRe: What's Wrong With This Style? Pin
Kevin Marois21-Nov-22 11:44
professionalKevin Marois21-Nov-22 11:44 
QuestionStyle Question Pin
Kevin Marois20-Nov-22 10:30
professionalKevin Marois20-Nov-22 10:30 
AnswerRe: Style Question Pin
Richard Deeming20-Nov-22 22:37
mveRichard Deeming20-Nov-22 22:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.