我正在使用mahapps metro应用程序在wpf中创建一个gui。我使用过代码
<Controls:Badged Badge="{Binding Path=BadgeValue}">
<!-- Control to wrap goes here -->
<Button Content="Notifications" />
</Controls:Badged>
说,如果我想在通知回调中更新'BadgeValue',我该怎么做呢?请帮助..
答案 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" />