0

ListView の列に CellTemplate があります。CellTemplate には、ItemTemplate を持つ ComboBox が含まれています。ItemsSource と SelectedItem の両方が別の ViewModel にバインドされています。ListView は、ViewModel の ObservableCollection にバインドされています。ListView の上には、選択した項目を上下に移動するためのボタンを備えたツールバーがあります。I ボタン​​は、ObservableCollection で Move を作成する ICommand にバインドされています。

ここに画像の説明を入力

ビューは正常に更新されますが、ComboBox で選択された項目は DataTemplate を使用しておらず、型名のみを表示しています。

ここに画像の説明を入力

IsEditable = false の場合、すべてが正常に機能していることがわかりましたが、これを true にする必要があります。

問題を検証する小さなプロジェクトを作成しました。おそらくこれはWPFの問題です。

XAML は次のとおりです。

<Window x:Class="WpfApplication3.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate DataType="{x:Type WpfApplication3:Item}">
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
    <DataTemplate x:Key="cellTemplate">
        <ComboBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" Width="100" IsEditable="true" TextSearch.TextPath="Name"/>
    </DataTemplate>
</Window.Resources>
<Grid>
    <StackPanel>
        <ToolBar>
            <Button Content="Add" Command="{Binding AddItemCommand}"/>
            <Button Content="Up" Command="{Binding MoveItemUpCommand}" CommandParameter="{Binding ElementName=listView, Path=SelectedItem}"/>
            <Button Content="Down" Command="{Binding MoveItemDownCommand}" CommandParameter="{Binding ElementName=listView, Path=SelectedItem}"/>
        </ToolBar>
        <ListView x:Name="listView" ItemsSource="{Binding Collection}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Name" CellTemplate="{StaticResource cellTemplate}"/>
                </GridView>
            </ListView.View>
        </ListView>
    </StackPanel>
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

public class ViewModel
{
    public ICommand AddItemCommand { get; private set; }
    public ICommand MoveItemUpCommand { get; private set; }
    public ICommand MoveItemDownCommand { get; private set; }
    public ObservableCollection<Row> Collection { get; set; }

    public ViewModel()
    {
        Collection = new ObservableCollection<Row>();
        AddItemCommand = new RelayCommand(AddItem);
        MoveItemUpCommand = new RelayCommand<Row>(MoveItemUp, CanMoveItemUp);
        MoveItemDownCommand = new RelayCommand<Row>(MoveItemDown, CanMoveItemDown);
    }

    private bool CanMoveItemDown(Row arg)
    {
        if (arg == null)
            return false;

        return Collection.Last() != arg;
    }

    private void MoveItemDown(Row obj)
    {
        var index = Collection.IndexOf(obj);

        Collection.Move(index, index + 1);
    }

    private bool CanMoveItemUp(Row arg)
    {
        if (arg == null)
            return false;

        return Collection.First() != arg;
    }

    private void MoveItemUp(Row row)
    {
        var index = Collection.IndexOf(row);

        Collection.Move(index, index - 1);
    }

    private void AddItem()
    {
        Collection.Add(new Row());
    }
}

public class Row
{
    public Row()
    {
        Items = new List<Item> { new Item { Name = "Test1" }, new Item { Name = "Test2" } };
    }

    public List<Item> Items { get; set; }

    public Item SelectedItem { get; set; }
}

public class Item
{
    public string Name { get; set; }

    public int Order { get; set; }
}
4

0 に答える 0