0

I try to change the Kind of a Packicon programmatically.

Updated Question:

That's my code now:

I implemented the Interface INotifyPropertyChagned now:

public partial class OwnExtendedMessageBox : Window, INotifyPropertyChanged

Then I added these lines

    //property of Binding changed
    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }

Then I call it like that

        switch (MessageBoxIcon)
        {
            case OwnMessageBoxIcon.None:
                MessageBoxIconKind = PackIconKind.Box;
                break;
            case OwnMessageBoxIcon.Info:
                MessageBoxIconKind = PackIconKind.Information;
                break;
            case OwnMessageBoxIcon.Warning:
                MessageBoxIconKind = PackIconKind.Warning;
                break;
            case OwnMessageBoxIcon.Error:
                MessageBoxIconKind = PackIconKind.Error;
                break;
            default:
                MessageBoxIconKind = PackIconKind.Box;
                break;
        }

        OnPropertyChanged("MessageBoxIconKind");

WPF:

<materialDesign:PackIcon Kind="{Binding MessageBoxIconKind}" Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}" ...

I also tryed it with a string, but it also doesn't worked

  • In my mind Kind is a string, have you tried to set a string like "Box", "Information". Binding a string – Ximaze C Oct 29 '20 at 09:20
  • Yes i have tried to set a string, but it doesn't worked. – StylezZxDmg Oct 29 '20 at 09:24
  • I'm not sure about the type (has it to be a string or an PackIconKind?). In any way, your code will never work: you have to raise a PropertyChange event to let the view (XAML) know you changed the PackIconKind property. You can see here how it works https://www.c-sharpcorner.com/article/explain-inotifypropertychanged-in-wpf-mvvm/ – PiGi78 Oct 29 '20 at 09:26
  • Right click on PackIconKind (an enumeration) and select definition. You need to parse the enumeration like this : PackIconKind kind = (PackIconKind)Enum.Parse(typeof(PackIconKind), "Box"); – jdweng Oct 29 '20 at 09:27
  • @PiGi78: So I have to send an event, every time the value of a Binding changes? – StylezZxDmg Oct 29 '20 at 09:42
  • I updated the question with INotifyPropertyChagned – StylezZxDmg Oct 29 '20 at 10:13
  • The ticket is closed so I can't post a reply with an example, If you need it, let me know how can I send it to you – PiGi78 Oct 29 '20 at 14:13

0 Answers0