WPF 更改 ViewModel.PropertyChanged 事件上的按钮内容

发布于 2024-09-09 23:24:11 字数 2014 浏览 6 评论 0原文

我的尝试(如下)失败:

<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 属性更改时更改按钮(它会触发 INotifyPropertyChanged.PropertyChanged,并以“IsReadOnly”作为 PropertyName)。我缺少什么?

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

My attempt (below) fails:

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

I'm trying to get the button to change when the IsReadOnly property on the ViewModel changes (it fires INotifyPropertyChanged.PropertyChanged with "IsReadOnly" as the PropertyName). What am I missing?

.NET 3.5

ANSWER (at least for me - doesn't support the general case):

I just wrote a quick converter for boolean property binding.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-09-16 23:24:11

如果您设置按钮的属性“Content”,则无法使用触发器更改它,因为第一个属性优先。
尝试删除内容的设置,它应该可以工作,因为触发器会自己做正确的工作。

If you set the property "Content" of the Button, you cannot change it with a trigger, because the first one takes precedence.
Try to remove the setting of Content and it should work, because triggers will do the right work themselves.

迷爱 2024-09-16 23:24:11

您使用的是 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>

用于 Window 的 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"));
        }
    }
}

You are using C# which means that the ToString() method for booleans return "true" and "false; whereas VB.NET they would be "True" and "False.

Your original code will work if you change your triggers to use the lowercase "true" and "false" instead of uppercase "True" and "False".

Tested with this code...

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# for the Window:

public MainWindow()
{
    InitializeComponent();
}
private void ChangeIsReadOnly_Click(object sender, RoutedEventArgs e)
{
    ViewModel theViewModel = (ViewModel)FindResource("TheViewModel");
    theViewModel.IsReadOnly = !theViewModel.IsReadOnly;
}

C# for the ViewModel class:

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"));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文