我使用xaml和C#创建了一个简单的应用程序,它将边框颜色绑定到boolean方法后面的代码IsToday
。但是不知何故,它不起作用。
有人可以帮忙吗?我也尝试过INotifyPropertyChanged
,但是没有用。感谢有人可以提供帮助,谢谢!
后面的代码:
namespace WpfApplication3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
IsToday = true;
InitializeComponent();
}
public bool IsToday { get; set; }
}
}
XAML
<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">
<Window.Resources>
<ResourceDictionary Source="Dictionary1.xaml">
</ResourceDictionary>
</Window.Resources>
<Grid>
<Border Style="{StaticResource Highlight}">
</Border>
</Grid>
</Window>
XAML字典
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Border" x:Key="Highlight">
<Style.Triggers>
<DataTrigger Binding="{Binding IsToday}" Value="True">
<Setter Property="Background" Value="Red"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsToday}" Value="False">
<Setter Property="Background" Value="Blue"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
您可以只从xaml设置上下文。
<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"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<!-- your code -->
</Window>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句