Wpf 在ObservableCollection中移动项目时,DataTemplate将消失
我在ListView中有一个列的CellTemplate。CellTemplate包含一个具有ItemTemplate的组合框。ItemsSource和SelectedItem都绑定到另一个ViewModel。 ListView绑定到ViewModel上的ObservableCollection。在ListView的上方有一个工具栏,其中包含上下移动所选项目的按钮。我按钮了一个绑定到和ICommand的按钮,它将在ObservableCollection上移动 视图更新得很好,但是组合框中的选定项没有使用DataTemplate,只是显示类型名称 我发现如果IsEditable=false,一切都正常,但我需要这是真的 我创建了一个小项目来验证这个问题。也许这是WPF中的一个问题 这里是XAML:Wpf 在ObservableCollection中移动项目时,DataTemplate将消失,wpf,mvvm,datatemplate,Wpf,Mvvm,Datatemplate,我在ListView中有一个列的CellTemplate。CellTemplate包含一个具有ItemTemplate的组合框。ItemsSource和SelectedItem都绑定到另一个ViewModel。 ListView绑定到ViewModel上的ObservableCollection。在ListView的上方有一个工具栏,其中包含上下移动所选项目的按钮。我按钮了一个绑定到和ICommand的按钮,它将在ObservableCollection上移动 视图更新得很好,但是组合框中的选
<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>
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
DataContext=新的ViewModel();
}
}
公共类视图模型
{
公共ICommand AddItemCommand{get;private set;}
public ICommand MoveItemUpCommand{get;private set;}
public ICommand moveitemdown命令{get;private set;}
公共ObservableCollection集合{get;set;}
公共视图模型()
{
集合=新的ObservableCollection();
AddItemCommand=新的RelayCommand(AddItem);
MoveItemUpCommand=新的RelayCommand(MoveItemUp,CanMoveItemUp);
MoveItemDownCommand=新的RelayCommand(MoveItemDown,CanMoveItemDown);
}
私有布尔值CanMoveItemDown(行参数)
{
如果(arg==null)
返回false;
返回集合。Last()!=arg;
}
私有void MoveItemDown(行obj)
{
var索引=Collection.IndexOf(obj);
集合。移动(索引,索引+1);
}
私有布尔CanMoveItemUp(行arg)
{
如果(arg==null)
返回false;
返回集合。First()!=arg;
}
私有void MoveItemUp(行)
{
var index=Collection.IndexOf(行);
集合。移动(索引,索引-1);
}
私有void AddItem()
{
Collection.Add(新行());
}
}
公共类行
{
公共行()
{
Items=newlist{newitem{Name=“Test1”},newitem{Name=“Test2”};
}
公共列表项{get;set;}
公共项SelectedItem{get;set;}
}
公共类项目
{
公共字符串名称{get;set;}
公共整数顺序{get;set;}
}
显示的类型名称:WpfApplication3.Row
或WpfApplication3.Item
?此外,我发现您的视图模型(包括行
和项
)都没有实现INotifyPropertyChanged
;INotifyPropertyChanged
是否也会出现此问题?文本显示的是WpfApplication3.Item。在这种情况下,不必实现INotifyPropertyChanged,因为视图接收到由ObservableCollection更改的OnCollection。
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; }
}