[英]WPF - Set SelectedItem property using the ItemsSource property
I am trying to override the RadDataFilter ControlTemplates and I used the Custom UserControl. 我试图覆盖RadDataFilter ControlTemplates,并且使用了自定义UserControl。 I have 2 dependency properties in my class:
我的课堂上有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);
}
}
and in my XAML: 在我的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>
The place where I am using this control is here: 我使用此控件的地方在这里:
<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}" />
where the SimpleFilter.AvailableMembers and SimpleFilter.SelectedMember are part of Telerik Source Code. 其中SimpleFilter.AvailableMembers和SimpleFilter.SelectedMember是Telerik源代码的一部分。
Now my question is, how do I set the SelectedItem property from my CustomControl? 现在我的问题是,如何从CustomControl设置SelectedItem属性?
Please let me know. 请告诉我。
Got the binding to work, somehow the binding was not happening for SimpleFilter.SelectedMember. 使绑定正常工作,以某种方式,对于SimpleFilter.SelectedMember而言,绑定并未发生。 Instead I had binding set for the SimpleFilter object and then setting its properties from the Custom Control.
相反,我为SimpleFilter对象设置了绑定,然后从“自定义控件”设置其属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.