据我了解,WinRT 不支持 DataTriggers。但是,如何解决这个问题?
我有一个实现了“选定”属性的模型,扩展了 INotifyPropertyChanged。如果 Selected 为 True,我想将按钮的边框更改为红色。我只是不知道如何将属性更改链接到触发状态更改。
模型
class MyObject : INotifyPropertyChanged
{
public MyObject()
{
Selected = true;
}
private bool _selected;
public bool Selected
{
get { return _selected; }
set { _selected = value; OnPropertyChanged("Selected"); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
XAML
<Grid>
<Button Margin="131,97,171,124" Content="Hello!" DataContext="MyObject" d:DataContext="{d:DesignInstance local:MyObject}">
<Button.Template>
<ControlTemplate TargetType="Button">
<Border BorderThickness="1">
<Border.BorderBrush>
<SolidColorBrush Color="Aqua"/>
</Border.BorderBrush>
<StackPanel>
<TextBlock Text="{TemplateBinding Content}"/>
<TextBlock Text="{Binding Selected}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
</Grid>