![](/img/trans.png)
[英]WPF `ComboBox` ItemsSource property is changed the SelectedItem property is being set to null
[英]WPF - Set SelectedItem property using the ItemsSource property
我试图覆盖RadDataFilter ControlTemplates,并且使用了自定义UserControl。 我的课堂上有2个依赖项属性:
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(CustomControl), new UIPropertyMetadata());
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource"
, typeof(IEnumerable)
, typeof(CustomControl));
//, new PropertyMetadata(RadDataFilter.OnItemsSourcePropertyChanged));
public IEnumerable ItemsSource
{
get
{
return this.GetValue(ItemsSourceProperty) as IEnumerable;
}
set
{
this.SetValue(ItemsSourceProperty, value);
}
}
在我的XAML中:
<UserControl x:Class="RADDataFilterExample.CustomControl"
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"
mc:Ignorable="d" >
<StackPanel Orientation="Horizontal">
<Button Content="Shankar" Click="Button_Click" />
</StackPanel>
</UserControl>
我使用此控件的地方在这里:
<local:CustomControl x:Name="PART_SimpleFilterMemberComboBox" Content="Finally!!"
Margin="0,0,3,0" MinWidth="100" VerticalAlignment="Center"
ItemsSource="{Binding SimpleFilter.AvailableMembers}"
SelectedItem="{Binding SimpleFilter.SelectedMember, Mode=TwoWay}" />
其中SimpleFilter.AvailableMembers和SimpleFilter.SelectedMember是Telerik源代码的一部分。
现在我的问题是,如何从CustomControl设置SelectedItem属性?
请告诉我。
使绑定正常工作,以某种方式,对于SimpleFilter.SelectedMember而言,绑定并未发生。 相反,我为SimpleFilter对象设置了绑定,然后从“自定义控件”设置其属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.