2017-06-16 116 views
1

我正在使用mahapps metro应用程序在wpf中制作gui。我已经使用的代码Mahapps metro徽章控制如何更新徽章的价值

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

说,如果我要在通知回调更新“BadgeValue”,我怎么会去这样做? plz帮助..

回答

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