我试图在属性值更改时更改控件的外观。我正在使用下面的XAML和C#代码,但是当我点击更改Property 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;
}
}
}
答案 0 :(得分:3)
如果您不使用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;
}
}