0

Observable CollectionofPersonにデータバインドされたListBoxを含むビューがあります。同じビューで、私はボタンを持っています。リストボックスからアイテムを選択し、ボタンをクリックすると、選択したアイテムの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}"/>
4

1 に答える 1

0

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

}

人物クラス:

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));
        }
    }
}
于 2013-03-01T01:19:14.723 に答える