英文:
Inserting new item on ObservableCollection doesn't reflect on view, used DependencyProperty
问题
为什么在VM的People集合中插入新项,不会在视图中反映出来?
如果不使用依赖属性,它可以工作,但我需要在依赖属性的用户控件中使用这个集合。
英文:
My MainWindow.xml
<Window x:Class="WpfApp5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.DataContext>
<local:vm/>
</Window.DataContext>
<StackPanel>
<Button Content="Click" Command="{Binding addCommand}"/>
<local:ModifiedControlView Items="{Binding People}" />
</StackPanel>
</Window>
My MainViewModel
public partial class vm : ObservableObject
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public ObservableCollection<Person> People { get; set; } = new ObservableCollection<Person>();
public vm()
{
}
[RelayCommand]
private void add()
{
People.Add(new Person { Name = "New", Age = 0 });
}
}
ModifiedControlView.xaml
<UserControl x:Class="WpfApp5.ModifiedControlView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp5"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<ListView ItemsSource="{Binding}">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</UserControl>
ModifiedControlView.xaml.c
public partial class ModifiedControlView : UserControl
{
public ModifiedControlView()
{
InitializeComponent();
}
public static readonly DependencyProperty ItemsProperty =
DependencyProperty.Register("Items", typeof(ObservableCollection<Person>), typeof(ModifiedControlView), new PropertyMetadata(null));
public ObservableCollection<Person> Items
{
get { return (ObservableCollection<Person>)GetValue(ItemsProperty); }
set { SetValue(ItemsProperty, value); }
}
}
Why inserting new item on People Collection in VM, doesn't reflect in the view?
If I don't use the dependency property it works, but I need to use this collection inside a usercontrol with dependency property.
答案1
得分: 1
你想将UserControl的XAML中的ListView的ItemsSource
绑定到UserControl的Items
属性上:
<ListView ItemsSource="{Binding Items, RelativeSource={RelativeSource AncestorType=UserControl}}">
作为注意,将Items
属性声明为ObservableCollection<Person>
并不是必需的。
使用IEnumerable<Person>
作为属性类型会更灵活。如果在视图模型中使用ObservableCollection<Person>
作为源属性类型,关于集合变化的通知事件仍然会传播到控件中。
英文:
You want to bind the ItemsSource
of the ListView in the UserControl's XAML to the Items
property of the UserControl:
<ListView ItemsSource="{Binding Items,
RelativeSource={RelativeSource AncestorType=UserControl}}">
As a note, it is not necessary to declare the Items
property as an ObservableCollection<Person>
.
It would provide greater flexibilty to use IEnumerable<Person>
as property type. With an ObservableCollection<Person>
as the source property type in the view model, notification events about collection changed would still be propagated to the control.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论