1

这是xml:

<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";
  }

}

4

1 回答 1

0

我无法告诉您尝试使用触发器更新/修改什么。
如果以下内容没有帮助,也许您可​​以提供更多详细信息。

如果您按如下方式修改 DataTemplate,您将进入 Converter。

<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 实现了转换器。
就我而言,我返回了一个串联的姓名和年龄字符串。

于 2010-10-13T13:33:48.343 回答