单击按钮更改选定的列表框项数据模板

本文关键字:数据 列表 按钮 单击 | 更新日期: 2023-09-27 18:01:43

我有一个绑定到Person的Observable Collection的ListBox视图。在同一个视图中,我有一个Button。我想做的是从列表框中选择一个项目,并在点击按钮时将所选项目的DataTemplate从"Person"替换为"PersonEdit"。

我已经看过TemplateSelector, Triggers等,但似乎不能得到任何工作。

任何想法?

<DataTemplate x:Key="Person">
        <TextBlock Text="{Binding Path=Name}"/>
    </DataTemplate>
    <DataTemplate x:Key="PersonEdit">
        <TextBox Text="{Binding Path=Name}"/>
    </DataTemplate>
<ListBox
        x:Name="lbPersons"
        Grid.Row="0"
        VerticalAlignment="Stretch"
        Margin="5"
        ItemsSource="{Binding Path=Persons}"
        ItemTemplate="{StaticResource ResourceKey=Person}"
        >
    </ListBox>
<Button
        Grid.Row="1"
        VerticalAlignment="Center"
        HorizontalAlignment="Center"
        Content="Add Person" Command="{Binding AddPerson}"/>

单击按钮更改选定的列表框项数据模板

我认为最好的选择是在你的Person模型中添加一个属性,告诉UI你正在编辑并使用该属性更改Template

这里是我的意思的一个粗略的工作示例,在这个示例中,当您将Person项设置为IsEditing时,它将将Template更改为PersonEdit,否则它将设置Template Person

Xaml:

<Window x:Class="WpfApplication7.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="399" Width="464" Name="UI" >
    <Window.Resources>
        <DataTemplate x:Key="Person">
            <TextBlock Text="{Binding Name}"/>
        </DataTemplate>
        <DataTemplate x:Key="PersonEdit">
            <TextBox Text="{Binding Name}"/>
        </DataTemplate>
        <Style TargetType="{x:Type ListBoxItem}" x:Key="PersonStyle">
            <Setter Property="ContentTemplate" Value="{StaticResource Person}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEditing}" Value="True">
                    <Setter Property="ContentTemplate" Value="{StaticResource PersonEdit}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid DataContext="{Binding ElementName=UI}"> 
        <ListBox Margin="5,5,5,30"
                 ItemsSource="{Binding Persons}"
                 SelectedItem="{Binding SelectedPerson}"
                 ItemContainerStyle="{StaticResource PersonStyle}" />
        <Button Margin="5,333,369,5" Content="Add Person" Click="Button_Click" />
    </Grid>
</Window>
代码:

public partial class MainWindow : Window
{
    private Person _selectedPerson;
    private ObservableCollection<Person> _persons = new ObservableCollection<Person>();
    public MainWindow()
    {
        InitializeComponent();
        Persons.Add(new Person { Name = "Stack" });
        Persons.Add(new Person { Name = "Overflow" });
    }
    public ObservableCollection<Person> Persons
    {
        get { return _persons; }
        set { _persons = value; }
    }
    public Person SelectedPerson
    {
        get { return _selectedPerson; }
        set { _selectedPerson = value; }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SelectedPerson.IsEditing = true;
    }
}

Person类:

public class Person : INotifyPropertyChanged
{
    private string _name;
    private bool _isEditing;
    public string Name
    {
        get { return _name; }
        set { _name = value; NotifyPropertyChanged("Name"); }
    }
    public bool IsEditing
    {
        get { return _isEditing; }
        set { _isEditing = value; NotifyPropertyChanged("IsEditing"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}