[英]Bind to a property of a collection declared in xaml
There is a UserControl with the property - a collection of class Item, in Item is dep property Name. 有一个具有属性的UserControl-类Item的集合,其中Item是dep属性Name。 There ViewModel, property that needs bind to Name of Item. 还有ViewModel,需要绑定到Name的属性。 ViewModel is in the UserControl DataContext. ViewModel在UserControl DataContext中。
code: 码:
public partial class UserControl1
{
public ObservableCollection<Item> Items { get; set; }
...
public class Item : DependencyObject
{
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Item), new
PropertyMetadata(null));
}
public class ViewModel
{
public string Name { get; set; }
}
userControl.DataContext = new ViewModel { Name = "Test" };
<wpfApplication6:UserControl1 x:Name="userControl">
<wpfApplication6:UserControl1.Items>
<wpfApplication6:Item Name="{Binding Name, RelativeSource={RelativeSource FindAncestor,
AncestorType=wpfApplication6:UserControl1}}" />
</wpfApplication6:UserControl1.Items>
</wpfApplication6:UserControl1>
There is not work. 没有工作。 Please, help me. 请帮我。
<wpfApplication6:UserControl1 x:Name="userControl">
<wpfApplication6:UserControl1.Items>
<wpfApplication6:Item Name="{Binding Path=DataContext.Name, RelativeSource={RelativeSource FindAncestor,
AncestorType=wpfApplication6:UserControl1}}" />
</wpfApplication6:UserControl1.Items>
</wpfApplication6:UserControl1>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.