DataTrigger不会进入转换器

时间:2010-10-13 11:04:49

标签: wpf datatrigger imultivalueconverter

这是xaml:

<Window x:Class="WpfApplication4.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:WpfApplication4" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1"
    Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <Grid.Resources>
        <local:MultiConverter x:Key="con" />
    </Grid.Resources>

    <ListView ItemsSource="{Binding Persons}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <DataTemplate.Triggers>
                    <DataTrigger Value="True">
                        <DataTrigger.Binding>
                            <MultiBinding Converter="{StaticResource con}">
                                <Binding Path="Name" />
                                <Binding Path="Age" />
                            </MultiBinding>
                        </DataTrigger.Binding>
                    </DataTrigger>
                </DataTemplate.Triggers>

                <TextBlock Text="{Binding Name}">

                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

    <Button Grid.Row="1" Click="Button_Click" ></Button>
</Grid>

这是window1的代码隐藏:

 public partial class Window1 : Window {

  public List<Person> Persons { get; set; }

  public Window1() {

     Persons = new List<Person>()
     {
        new Person(){Name="Keli",Age=1},         
        new Person(){Name="Keli",Age=2},         
        new Person(){Name="Tom",Age=3},         
        new Person(){Name="Keli",Age=4},         
        new Person(){Name="Keli",Age=5},         
     };

     InitializeComponent();
     DataContext = this;
  }

  private void Button_Click(object sender, RoutedEventArgs e) {
     Persons[0].Name = "Tom";
  }

}

1 个答案:

答案 0 :(得分:0)

我无法告诉您尝试使用触发器更新/修改的内容 如果以下内容无效,您可以提供更多详细信息。

如果您按如下方式修改DataTemplate,您将进入转换器。

<ListView ItemsSource="{Binding Persons}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource con}">
                        <Binding Path="Name" />
                        <Binding Path="Age" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

我假设您使用IMultiValueConverter实现了转换器 在我的情况下,我返回了一个名称和年龄的串联字符串。