[英]C# WPF: Dependency Property vs MVVM
I need to create a component with Dependency Property, I don´t know how to bind the data from another user control, can someone explain it to me?我需要创建一个具有依赖属性的组件,我不知道如何从另一个用户控件绑定数据,有人可以向我解释一下吗? I don´t think i need to create a viewmodel for the component.
我认为我不需要为组件创建一个视图模型。
Here´s my code:这是我的代码:
Here I call the component这里我调用组件
<DockPanel Grid.Row="1">
<ctrls:CustomComboBox Collection="{Binding Path=TestingCollection}" SelectedCollectionItem="{Binding Path=SelectedItem}"
CreateCommand="" DeleteCommand="" UpdateCommand=""/>
</DockPanel>
And Here it´s my component这是我的组件
<ComboBox Grid.Column="0" SelectedValue="{Binding SelectedCollectionItem,ElementName=CustomComboBoxControl, Mode=TwoWay}"
ItemsSource="{Binding Collection}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
/>
Here it is my xaml.cs这是我的 xaml.cs
public static readonly DependencyProperty CollectionProperty =
DependencyProperty.Register("Collection", typeof(ObservableCollection<object>), typeof(CustomComboBox));
public ObservableCollection<object> Collection
{
get { return (ObservableCollection<object>)GetValue(CollectionProperty); }
set { SetValue(CollectionProperty, value); }
}
public static readonly DependencyProperty SelectedCollectionItemProperty =
DependencyProperty.Register("SelectedCollectionItem", typeof(object), typeof(CustomComboBox));
public object SelectedCollectionItem
{
get { return (object)GetValue(SelectedCollectionItemProperty); }
set { SetValue(SelectedCollectionItemProperty, value); }
}
EDIT:编辑:
I tryed creating a VM and setting the data context, and works, but dependency properties seem pointless我尝试创建一个虚拟机并设置数据上下文,并且工作正常,但依赖属性似乎毫无意义
EDIT 2:编辑2:
I succeded, i delete my VM i was lacking and ElementName in ItemSource我成功了,我删除了我缺少的 VM 和 ItemSource 中的 ElementName
我成功了,我删除了我缺少的 VM 和 ItemSource 中的 ElementName
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.