【问题标题】:Collection in custom control does not update view wpf自定义控件中的集合不会更新视图 wpf
【发布时间】:2017-03-24 06:27:50
【问题描述】:

有一些自定义控件经验的人可以帮忙吗????

你能看出为什么我的收藏没有更新吗?

基本上我有 2 个列表框,当我在两者之间移动项目时 项目的数量没有下降。(属性不更新)

调试自定义控件控件内的属性是否正确, 但是当它传播到使用自定义控件的视图时,它不会!。

可以下载项目的链接。

https://1drv.ms/f/s!AmyWRgk_dFxGaohoQA_v-aulUOE

我很近,但很远。 我不想发布大量代码却没有得到回复。 项目很小。

我尝试过默认设置绑定,但不起作用。

建议?

CustomControl(2 个列表框和左右移动项目的按钮)

generic.xaml

    <ListBox Name="PART_lstLeft" 
          MinHeight="200"
          SelectionMode="Extended"
          ItemsSource="{Binding LeftItems, 
                            RelativeSource={RelativeSource TemplatedParent},
                            Mode=TwoWay}"
          IsSynchronizedWithCurrentItem="True"
          SelectedIndex="0" />

     same for the right listbox

--xaml 结束

自定义控件代码

    public MultipleListControl()
    {
        LeftItems = new ObservableCollection<object>();
        RightItems = new ObservableCollection<object>();
    }

    public static readonly DependencyProperty LeftItemsProperty =
                DependencyProperty.Register("LeftItems",
                    typeof(IEnumerable<object>),
                    typeof(MultipleListControl), new UIPropertyMetadata(null));

    public IEnumerable<object> LeftItems
    {
            get { return (IEnumerable<object>)GetValue(LeftItemsProperty); }
            set { SetValue(LeftItemsProperty, value); }
    }


      public static readonly DependencyProperty RightItemsProperty =
               DependencyProperty.Register("RightItems",
           typeof(IEnumerable<object>),
           typeof(MultipleListControl), new UIPropertyMetadata(null));

    public IEnumerable<object> RightItems
    {
        get { return (IEnumerable<object>)GetValue(RightItemsProperty); }
        set { SetValue(RightItemsProperty, value); }
    }

    etc.... rest irrelevant

视图模型

    private ObservableCollection<CustomerViewModel> availableCustomers;
    public ObservableCollection<CustomerViewModel> AvailableCustomers
    {
        get { return availableCustomers; }
        set
        {
            availableCustomers = value;
            OnPropertyChanged("AvailableCustomers");
        }
    }

    private ObservableCollection<CustomerViewModel> selectedCustomers;
    public ObservableCollection<CustomerViewModel> SelectedCustomers
    {
        get { return selectedCustomers; }
        set
        {
            selectedCustomers = value;
            OnPropertyChanged("SelectedCustomers");
        }
    }

查看

  <Window.Resources>
    <viewModels:CustomerSelectorViewModel x:Key="ViewModel" />
  </Window.Resources>        

         <multipleList:MultipleListControl  
                    Name="MultipleListControl1" 
                   LeftItems="{Binding Source=
                   {StaticResource ViewModel},
                   Path=AvailableCustomers,Mode=TwoWay}"

                   RightItems="{Binding Source={StaticResource ViewModel},
                   Path=SelectedCustomers,Mode=TwoWay}" />

【问题讨论】:

    标签: wpf custom-controls


    【解决方案1】:

    首先是你的列表框; ItemsSource="{TemplateBinding LeftItems,

    支持你的观点; LeftItems="{Binding AvailableCustomers 如果您的视图模型是在数据上下文中设置的

    应该看看另一个代码,因为你似乎做得不对 custom 以及使用它的解决方案solution

    【讨论】:

    • 感谢您抽出宝贵时间,如果您能抽出几分钟时间来解决这个问题,我将不胜感激,因为它已经困扰了我一段时间。我正在阅读有关自定义控件等的信息。但与此同时,我需要对这一项进行排序,项目已绑定但集合未更新
    • 试过你的改变,但还是不行,收藏没有更新。有什么想法吗?
    • ViewModel 是如何定义的?为什么是静态的? ? LeftItems="{Binding Source={StaticResource ViewModel ???
    • 它是 LeftItems="{Binding Source= {StaticResource ViewModel}, Path=AvailableCustomers,模式=TwoWay}"
    • 撤消视图更改;似乎还可以;运行时visual的输出选项卡中是否有任何绑定错误?
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-25
    • 2012-05-22
    • 2020-09-21
    • 1970-01-01
    相关资源
    最近更新 更多