Ada*_*dam 2 .net c# vb.net silverlight wpf
我有一个简单的用户控件,它围绕AutoCompleteBox包装一些逻辑.这个问题可以应用于任何ItemsControl控件,如下拉列表或列表框.
<UserControl>
<Grid Background="White">
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding myData}" />
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
我想SelectedItem在使用控件的父级中公开AutoCompleteBox 的属性.我的用户控件使用视图模型作为其数据上下文.否则,我想我可以将SelectedItem绑定到用户控件的依赖项属性.
当子自动完成框选择项目发生更改时,我需要能够检测父项.我怎么能这样做?
只需在UserControl中创建一个依赖项属性,并将其与内部控件绑定,如下所示:
1)在CustomControl中添加依赖项属性:
public System.Collections.IEnumerable ItemsSource
{
get { return (System.Collections.IEnumerable)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
// Using a DependencyProperty as the backing store for ItemsSource. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(System.Collections.IEnumerable), typeof(UserControl1), new UIPropertyMetadata(null));
Run Code Online (Sandbox Code Playgroud)
2)使用依赖属性绑定内部控件:
<UserControl ... x:Name="control" ... >
<ListBox ItemsSource="{Binding ElementName=control, Path=ItemsSource}" />
Run Code Online (Sandbox Code Playgroud)
编辑:实现ItemsSource