WPF更改ViewModel.PropertyChanged事件上的按钮内容
我的尝试(下文)失败:
<Canvas x:Key="Lock" ... /> <Canvas x:Key="Unlock" ... /> <Style x:Key="LockButtonStyle" TargetType="{x:Type Button}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True"> <DataTrigger.Setters> <Setter Property="Content" Value="{StaticResource Lock}" /> </DataTrigger.Setters> </DataTrigger> <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="False"> <DataTrigger.Setters> <Setter Property="Content" Value="{StaticResource Unlock}" /> </DataTrigger.Setters> </DataTrigger> </Style.Triggers> </Style> ... <Button Content="{StaticResource Lock}" />
我试图让ViewModel上的IsReadOnly属性发生变化时(这会使用“IsReadOnly”作为PropertyName触发INotifyPropertyChanged.PropertyChanged)更改按钮。 我错过了什么?
.NET 3.5
答案(至少对我来说 - 不支持一般情况):
我刚刚为布尔属性绑定写了一个快速转换器。
[ValueConversion(typeof(bool), typeof(object))] public class BooleanValueConverter : IValueConverter { public object FalseValue { get; set; } public object TrueValue { get; set; } #region IValueConverter Members public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (bool)value ? this.TrueValue : this.FalseValue; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return object.Equals(this.TrueValue, value) ? true : false; } #endregion } ... <local:BooleanValueConverter x:Key="LockOrUnlock" TrueValue="{StaticResource Unlock}" FalseValue="{StaticResource Lock}" /> ... <Button Content="{Binding Path=IsReadOnly, Converter={StaticResource LockOrUnlock}}" />
如果设置Button的属性“Content”,则不能用触发器更改它,因为第一个优先。
尝试删除内容的设置,它应该工作,因为触发器会自己做正确的工作。
你使用的是C#,这意味着布尔值的ToString()方法返回“true”和“false”,而VB.NET则是“True”和“False”。
如果您将触发器更改为使用小写“true”和“false”而不是大写“True”和“False”,则原始代码将起作用。
测试这个代码...
XAML:
<Window.Resources>
<local:ViewModel x:Key="TheViewModel" />
<Canvas x:Key="Lock">
<TextBlock Text="Lock"/>
</Canvas>
<Canvas x:Key="Unlock">
<TextBlock Text="Unlock"/>
</Canvas>
<Style x:Key="LockButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsReadOnly}" Value="true">
<DataTrigger.Setters>
<Setter Property="Content" Value="{StaticResource Lock}" />
</DataTrigger.Setters>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsReadOnly}" Value="false">
<DataTrigger.Setters>
<Setter Property="Content" Value="{StaticResource Unlock}" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid DataContext="{StaticResource TheViewModel}">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Button x:Name="LockButton" Style="{StaticResource LockButtonStyle}" />
<Button x:Name="ChangeIsReadOnly" Content="Change State" Grid.Row="1" Click="ChangeIsReadOnly_Click"/>
</Grid>
C#的窗口:
public MainWindow()
{
InitializeComponent();
}
private void ChangeIsReadOnly_Click(object sender, RoutedEventArgs e)
{
ViewModel theViewModel = (ViewModel)FindResource("TheViewModel");
theViewModel.IsReadOnly = !theViewModel.IsReadOnly;
}
ViewModel类的C#:
public class ViewModel : System.ComponentModel.INotifyPropertyChanged
{
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
private bool isReadOnly;
public bool IsReadOnly
{
get
{
return isReadOnly;
}
set
{
isReadOnly = value;
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("IsReadOnly"));
}
}
}
链接地址: http://www.djcxy.com/p/44655.html
上一篇: WPF change Button Content on ViewModel.PropertyChanged event