在你的视图模型中很容易做到这一点:
public class MainViewModel
{
private readonly Lazy<ObservableCollection<Person>> _people = new Lazy<ObservableCollection<Person>>(
() =>
{
return new ObservableCollection<Person>
{
new Person {Department = "Dept1", FirstName = "Person", LastName = "1"},
new Person {Department = "Dept2", FirstName = "Person", LastName = "2"},
};
});
public ObservableCollection<Person> People { get { return _people.Value; } }
public string Department
{
set
{
foreach (var p in People)
p.Department = value;
}
}
}
示例 XAML:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding Department, UpdateSourceTrigger=PropertyChanged}" />
<ListView ItemsSource="{Binding People}" Grid.Column="1">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="0,0,0,15">
<TextBlock Text="{Binding Department}"/>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>