Mahapps metro badged 控件如何更新徽章的值

Mahapps metro badged control how to update value of badge

我正在使用 mahapps metro 应用程序在 wpf 中制作图形用户界面。我用过代码

<Controls:Badged Badge="{Binding Path=BadgeValue}">
  <!-- Control to wrap goes here -->
  <Button Content="Notifications" />
</Controls:Badged>

比如说,如果我想在通知回调中更新 'BadgeValue',我该怎么做呢?请帮助..

您在 XAML 中设置绑定到的 BadgeValue 源 属性 并引发 PropertyChanged 事件,就像更新任何其他数据绑定一样属性.

给你举个例子:

查看模型:

public class ViewModel : INotifyPropertyChanged
{
    private string _badgeValue;
    public string BadgeValue
    {
        get { return _badgeValue; }
        set { _badgeValue = value; NotifyPropertyChanged(); }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    ViewModel viewModel = new ViewModel();
    public MainWindow()
    {
        InitializeComponent();
        DataContext = viewModel;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        viewModel.BadgeValue = "new value...";
    }
}

MainWindow.xaml:

<Controls:Badged Badge="{Binding Path=BadgeValue}">
    <Button Content="Notifications" />
</Controls:Badged>
<Button Content="Update" Click="Button_Click" />