Inserting new item on ObservableCollection doesn't reflect on view, used DependencyProperty

huangapple go评论115阅读模式
英文:

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&lt;Person&gt;并不是必需的。

使用IEnumerable&lt;Person&gt;作为属性类型会更灵活。如果在视图模型中使用ObservableCollection&lt;Person&gt;作为源属性类型,关于集合变化的通知事件仍然会传播到控件中。

英文:

You want to bind the ItemsSource of the ListView in the UserControl's XAML to the Items property of the UserControl:

&lt;ListView ItemsSource=&quot;{Binding Items,
                        RelativeSource={RelativeSource AncestorType=UserControl}}&quot;&gt;

As a note, it is not necessary to declare the Items property as an ObservableCollection&lt;Person&gt;.

It would provide greater flexibilty to use IEnumerable&lt;Person&gt; as property type. With an ObservableCollection&lt;Person&gt; as the source property type in the view model, notification events about collection changed would still be propagated to the control.

huangapple
  • 本文由 发表于 2023年8月11日 02:34:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定