“Setter.Property”必须具有非null值

时间:2013-05-13 06:10:45

标签: wpf xaml data-binding multidatatrigger

我正在阅读MSDN网站上的this文章,以便了解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,一切正常。

谢谢, 与Hemant

1 个答案:

答案 0 :(得分:4)

使用属性语法而不是属性元素语法

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
    ...
    </MultiDataTrigger.Conditions>
    <Setter TargetName="myBorder" Property="Background" Value="Green"/>
</MultiDataTrigger>