在ObservableCollection中移动项目时,DataTemplate会消失

时间:2012-06-26 11:42:56

标签: wpf mvvm datatemplate

我在ListView中有一个CellTemplate用于列。 CellTemplate包含一个ComboBox,它有一个ItemTemplate。 ItemsSource和SelectedItem都绑定到另一个ViewModel。 ListView绑定到ViewModel上的ObservableCollection。在ListView上方有一个工具栏,其中包含用于上下移动所选项目的按钮。我按下一个绑定和ICommand,它将在ObservableCollection上进行移动。

enter image description here

视图更新正常,但ComboBox中的选定项目未使用DataTemplate,只显示类型名称。

enter image description here

我发现如果IsEditable = false,一切正常,但我需要这是真的。

我创建了一个验证问题的小项目。也许这是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; }
}

0 个答案:

没有答案