c# - DataTrigger 绑定(bind)到属性

标签 c# wpf button styles datatrigger

我试图在属性值更改时更改控件的外观。我使用下面的 XAML 和 C# 代码,但是当我单击更改属性 myProperty 值的按钮时,没有任何反应,除非我第一次运行应用程序。有什么建议么?

 <Window x:Class="WpfApplication3.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Button Height="34" HorizontalAlignment="Left" x:Name="toggleBroadcast" VerticalAlignment="Top" Width="133" Margin="180,0,0,0">
            <Button.Style>
                <Style x:Name="bb" TargetType="{x:Type Button}">
                    <Setter Property="Content" Value="Original Content"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="true">
                            <Setter Property="Content" Value="IS TRUE"/>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=myProperty}" Value="false">
                            <Setter Property="Content" Value="IS FALSE"/>
                        </DataTrigger>
                    </Style.Triggers>

                </Style>
            </Button.Style>
         </Button>
        <Button Content="Click on This Button" HorizontalAlignment="Left" Width="158" Click="Button_Click_1" Height="34" VerticalAlignment="Top"/>
   </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication3
{
    public partial class MainWindow : Window
    {
        Boolean _myProperty = false;
        public MainWindow()
        {
            DataContext = this;
            InitializeComponent();
        }
        public Boolean myProperty
        {
            get
            {
                return _myProperty;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            _myProperty = !_myProperty;
        }
    }
}

最佳答案

如果您不使用DependencyProperty,则必须实现INotifyPropertyChanged,以便它可以通知 Xaml 属性已更改

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private bool _myProperty = false;
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }


    public bool myProperty
    {
        get { return _myProperty; }
        set { _myProperty = value; NotifyPropertyChanged("myProperty"); }
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    /// <summary>
    /// Notifies the property changed.
    /// </summary>
    /// <param name="property">The property name that changed.</param>
    public void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

或使用DependencyProperty

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }

    public bool myProperty
    {
        get { return (bool)GetValue(myPropertyProperty); }
        set { SetValue(myPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for myProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty myPropertyProperty =
        DependencyProperty.Register("myProperty", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false));


    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myProperty = !myProperty;
    }
}

关于c# - DataTrigger 绑定(bind)到属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14016979/

相关文章:

wpf - 如何在不知道进度动画的完成度的情况下显示进度动画?

Java AWT - 最后添加的按钮未显示

android - 创建带有点击外发光动画的android按钮

button - CSS 按钮样式来指示选中/未选中?

c# - 使用方法参数作为泛型类型参数

c# - 如何在 C# 中使用 JobStorage 获取所有 Hangfire 作业的列表?

c# - 字典和哈希表之间的区别

C#语音合成问题

c# - WPF中如何使用按钮导航到其他页面

c# - 嵌套或不嵌套的 if block ?