Mahapps地铁徽章控制如何更新徽章的价值

时间:2017-06-16 04:34:15

标签: c# wpf mahapps.metro

我正在使用mahapps metro应用程序在wpf中创建一个gui。我使用过代码

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

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

1 个答案:

答案 0 :(得分:1)

在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" />