【发布时间】: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