![](/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.