UserControl中嵌套DataGrid中的WPF SelectedItem属性 [英] WPF SelectedItem property in nested DataGrid in UserControl
问题描述
我有 UserControl
,让它称为 CustomDataGrid
,其中包含 DataGrid
。剩下的内容没关系。 SelectedItem
属性 DataGrid
必须是 SelectedItem
属性 CustomDataGrid
。我想要使用绑定
与此属性,因为我使用 MVVM
模式。所以我必须在 CustomDataGrid
中声明 SelectedItem
作为 DependencyProperty
。但是我没有任何想法,我可以正确地使它...
这是如何 DepedencyProperty
-s被声明通常:
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
SelectedItem,typeof(Object),typeof (CustomDataGrid),
new FrameworkPropertyMetadata(默认(Object),SelectedItemPropertyCallback)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
//可选
private static void SelectedItemPropertyCallback(DependencyObject obj,DependencyPropertyChangedEventArgs e)
{
// dataGrid - DataGrid嵌套在UserControl
((CustomDataGrid)obj).dataGrid.SelectedItem = e.NewValue;
}
//显然,它没有与嵌套的dataGrid的任何链接。这就是问题。
public Object SelectedItem
{
get {return GetValue(SelectedItemProperty); }
set {SetValue(SelectedItemProperty,value); }
}
那么,我如何声明 SelectedItem
属性正确?
您可以利用绑定框架将这些属性从底层对象连接到外部容器
假设 CustomDataGrid
为 UserControl
< pre class =lang-cs prettyprint-override>
class CustomDataGrid:UserControl
{
public CustomDataGrid()
{
Binding b = new Binding SelectedItem);
b.Source = this;
b.Mode = BindingMode.TwoWay;
dataGrid.SetBinding(DataGrid.SelectedItemProperty,b);
}
public object SelectedItem
{
get {return(object)GetValue(SelectedItemProperty); }
set {SetValue(SelectedItemProperty,value); }
}
//使用DependencyProperty作为SelectedItem的后备存储。这使得动画,样式,绑定等...
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register(SelectedItem,typeof(object),typeof(CustomDataGrid),new PropertyMetadata(null)) ;
}
我创建了一个名为 SelectedItem
在 CustomDataGrid
中,并设置一个双向绑定到 SelectedItem
的实际 dataGrid
所以这将连接这些属性,并将来回传播任何更改。
I have UserControl
, lets call it as CustomDataGrid
, that contains DataGrid
. Remained content doesn't matter. SelectedItem
property of DataGrid
must be SelectedItem
property of CustomDataGrid
. And I wanna be able to use Binding
with this property, cause I use MVVM
pattern. So I have to declare SelectedItem
as DependencyProperty
in CustomDataGrid
. But I have no ideas haw can I make it properly...
This is how DepedencyProperty
-s is declared usually:
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem", typeof(Object), typeof(CustomDataGrid),
new FrameworkPropertyMetadata(default(Object), SelectedItemPropertyCallback)
{
BindsTwoWayByDefault = true,
DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
// Optionally
private static void SelectedItemPropertyCallback(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
// dataGrid - `DataGrid` nested in `UserControl`
((CustomDataGrid)obj).dataGrid.SelectedItem = e.NewValue;
}
// Obviously, it has no any link with nested `dataGrid`. This is the problem.
public Object SelectedItem
{
get { return GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
So, how can I declare SelectedItem
property correctly?
You could leverage the binding framework for wiring such properties from underlying objects to outer containers
example assuming CustomDataGrid
as UserControl
class CustomDataGrid : UserControl
{
public CustomDataGrid()
{
Binding b = new Binding("SelectedItem");
b.Source = this;
b.Mode = BindingMode.TwoWay;
dataGrid.SetBinding(DataGrid.SelectedItemProperty, b);
}
public object SelectedItem
{
get { return (object)GetValue(SelectedItemProperty); }
set { SetValue(SelectedItemProperty, value); }
}
// Using a DependencyProperty as the backing store for SelectedItem. This enables animation, styling, binding, etc...
public static readonly DependencyProperty SelectedItemProperty =
DependencyProperty.Register("SelectedItem", typeof(object), typeof(CustomDataGrid), new PropertyMetadata(null));
}
I have created a property called SelectedItem
in CustomDataGrid
and set a two way binding to SelectedItem
of the actual dataGrid
inside.
so this will wire up these properties and will propagate any changes to and fro.
这篇关于UserControl中嵌套DataGrid中的WPF SelectedItem属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!