我正在 MSDN 网站上阅读这篇文章,以了解 DataTrigger。
我创建了一个看起来像这样的 DefaultViewModel 类。
namespace ControlTemplateDemo
{
public class DefaultViewModel
{
private List<ToDoItem> _list;
public DefaultViewModel()
{
_list = new List<ToDoItem>();
_list.Add(new ToDoItem { TaskName="Wedding",Priority = 1,Description="Important wedding",TypeOfTask = TaskType.Home});
_list.Add(new ToDoItem { TaskName = "Toyota Meeting", Priority = 3, Description = "WSR", TypeOfTask = TaskType.Work });
}
public List<ToDoItem> Tasks
{
get { return _list; }
}
}
}
我的 xamal 代码看起来像这样。
<Window x:Class="ControlTemplateDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ControlTemplateDemo"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:ToDoItem}">
<Border BorderThickness="1" Name="myBorder" Margin="5" Padding="5" BorderBrush="Aqua">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="Task Name" Grid.Row="0" Grid.Column="0"/>
<TextBlock Text="{Binding TaskName}" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Description:" Grid.Row="1" Grid.Column="0"/>
<TextBlock Text="{Binding Description}" Grid.Row="1" Grid.Column="1" />
<TextBlock Text="Priority:" Grid.Row="2" Grid.Column="0"/>
<TextBlock Text="{Binding Priority}" Grid.Row="2" Grid.Column="1"/>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=TypeOfTask}">
<DataTrigger.Value>
<local:TaskType>Home</local:TaskType>
</DataTrigger.Value>
<Setter TargetName="myBorder" Property="Background" Value="Yellow" />
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Priority}">
<Condition.Value>
<sys:Int32>3</sys:Int32>
</Condition.Value>
</Condition>
<Condition Binding="{Binding Path=Description}">
<Condition.Value>
<sys:String>WSR</sys:String>
</Condition.Value>
</Condition>
</MultiDataTrigger.Conditions>
<Setter>
<Setter.TargetName>myBorder</Setter.TargetName>
<Setter.Property>Background</Setter.Property>
<Setter.Value>Green</Setter.Value>
</Setter>
</MultiDataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<!--<local:DefaultViewModel x:Key="dvm"/>-->
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock Name="blah" FontSize="20" Text="My To do tasks."/>
<ListBox x:Name="lstTasks" ItemsSource="{Binding Path=Tasks}" HorizontalContentAlignment="Stretch" >
</ListBox>
</StackPanel>
</Grid>
Qeustion/问题:
我正在使用 DataTemplate 重新渲染 ListBox 中的数据。我的意图是根据多个条件突出显示边框的背景(列表项的边框)。例如,如果 TaskPriorty 为 3,TaskDescription 为“WSR”,那么我想用红色突出显示该列表项。但是,当我运行此应用程序时,我收到运行时错误并显示以下消息。
'Setter.Property' 必须具有非空值。
谁能帮我找出问题所在?没有 MultiDataTrigger 一切正常。
谢谢,赫曼特