Property wird nicht richtig geupdatet

  • WPF

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Plexian.

    Property wird nicht richtig geupdatet

    Hey Community,

    ich habe mal mein erstes WPF-Projekt gestartet und bin auf ein Problem gestoßen. In meinem Window habe ich Buttons, die nur sichtbar je nach Wert eines Enum-Felds sein sollen. Dieses Feld kann wiederum durch Buttons geändert werden. Nun wird zwar beim Klicken der Buttons das Feld geupdatet, aber die Visibility von den anderen Buttons ändert sich nicht. Ich kann mir schon denken, warum das nicht funktioniert (Die Buttons merken ja nix von der Änderung), aber ich weiß nicht wie man das fixen sollte.

    Hier mal notwendiger Code:

    XML-Quellcode

    1. <!-- Die sollen die Visibilty von den anderen Buttons beeinflussen -->
    2. <Button Grid.Column="0"
    3. Content="CUTS"
    4. Command="{Binding ChangeToShortCutPage}"/>
    5. <Button Grid.Column="1"
    6. Content="UNARY"
    7. Command="{Binding ChangeToUnaryPage}" />
    8. <Button Grid.Column="2"
    9. Content="POLYADIC"
    10. Command="{Binding ChangeToPolyadicPage}" />
    11. ...
    12. <!-- So sieht ein Button aus der bedingt sichtbar ist -->
    13. <Button Grid.Column="0"
    14. Grid.Row="2"
    15. Content="sin"
    16. Visibility="{Binding IsUnaryPageActivated, Converter={StaticResource BoolToVisibilityConverter}}" />


    C#-Quellcode

    1. class BoolToVisibilityConverter : IValueConverter
    2. {
    3. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    4. {
    5. return (bool)value ? Visibility.Visible : Visibility.Collapsed;
    6. }
    7. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    8. {
    9. throw new NotImplementedException();
    10. }
    11. }


    C#-Quellcode

    1. ​public MainViewModel()
    2. {
    3. _currentPage = ButtonPage.ShortCut;
    4. ChangeToShortCutPage = new RelayCommand(() => _currentPage = ButtonPage.ShortCut);
    5. ChangeToUnaryPage = new RelayCommand(() => _currentPage = ButtonPage.Unary);
    6. ChangeToPolyadicPage = new RelayCommand(() => _currentPage = ButtonPage.Polyadic);
    7. }
    8. enum ButtonPage
    9. {
    10. ShortCut,
    11. Unary,
    12. Polyadic
    13. }
    14. private ButtonPage _currentPage;
    15. public bool IsShortCutPageActivated => _currentPage == ButtonPage.ShortCut;
    16. public bool IsUnaryPageActivated => _currentPage == ButtonPage.Unary;
    17. public bool IsPolyadicPageActivated => _currentPage == ButtonPage.Polyadic;
    18. public ICommand ChangeToShortCutPage { get; private set; }
    19. public ICommand ChangeToUnaryPage { get; private set; }
    20. public ICommand ChangeToPolyadicPage { get; private set; }


    C#-Quellcode

    1. ​public class RelayCommand : ICommand
    2. {
    3. private readonly Func<bool> _canExecute;
    4. private readonly Action _execute;
    5. public event EventHandler CanExecuteChanged;
    6. public RelayCommand(Action execute) : this(execute, () => true)
    7. {
    8. }
    9. public RelayCommand(Action execute, Func<bool> canExecute)
    10. {
    11. _execute = execute;
    12. _canExecute = canExecute;
    13. }
    14. public bool CanExecute(object parameter)
    15. {
    16. return _canExecute == null || _canExecute.Invoke();
    17. }
    18. public void Execute(object parameter)
    19. {
    20. _execute.Invoke();
    21. }
    22. public void RaiseCanExecuteChanged()
    23. {
    24. CanExecuteChanged?.Invoke(this, EventArgs.Empty);
    25. }
    26. }



    Danke im Voraus!
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais
    Dein ViewModel muss INotifyPropertyChanged.PropertyChanged (im Setter der Property) feuern (​OnPropertyChanged(memberName)), sonst kriegen die davon natürlich nichts mit. Da gibt es nun sicherlich mehrere Ansätze, denn Du hast ja den Wert in Abhängigkeit von _currentPage.

    Grüße
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    @Trade okay danke dir, ich hab jz einfach aus dem private field ne public property gemacht, und feuere das da dann dreimal:

    C#-Quellcode

    1. public class MainViewModel : INotifyPropertyChanged
    2. {
    3. public MainViewModel()
    4. {
    5. _currentPage = ButtonPage.ShortCut;
    6. ChangeToShortCutPage = new RelayCommand(() => CurrentPage = ButtonPage.ShortCut);
    7. ChangeToUnaryPage = new RelayCommand(() => CurrentPage = ButtonPage.Unary);
    8. ChangeToPolyadicPage = new RelayCommand(() => CurrentPage = ButtonPage.Polyadic);
    9. }
    10. public enum ButtonPage
    11. {
    12. ShortCut,
    13. Unary,
    14. Polyadic
    15. }
    16. private ButtonPage _currentPage;
    17. public ButtonPage CurrentPage
    18. {
    19. get
    20. {
    21. return _currentPage;
    22. }
    23. set
    24. {
    25. _currentPage = value;
    26. OnPropertyChanged(nameof(IsShortCutPageActivated));
    27. OnPropertyChanged(nameof(IsUnaryPageActivated));
    28. OnPropertyChanged(nameof(IsPolyadicPageActivated));
    29. }
    30. }
    31. public bool IsShortCutPageActivated => _currentPage == ButtonPage.ShortCut;
    32. public bool IsUnaryPageActivated => _currentPage == ButtonPage.Unary;
    33. public bool IsPolyadicPageActivated => _currentPage == ButtonPage.Polyadic;
    34. public ICommand ChangeToShortCutPage { get; private set; }
    35. public ICommand ChangeToUnaryPage { get; private set; }
    36. public ICommand ChangeToPolyadicPage { get; private set; }
    37. public event PropertyChangedEventHandler PropertyChanged;
    38. protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    39. {
    40. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    41. }
    42. }


    In Ordnung so ? (Funktionieren tuts, geht nur um die Sauberkeit)
    »There's no need to "teach" atheism. It's the natural result of education without indoctrination.« — Ricky Gervais