0

There is a UserControl with the property - a collection of class Item, in Item is dep property Name. There ViewModel, property that needs bind to Name of Item. ViewModel is in the 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.

ortuomka
  • 23
  • 3

1 Answers1

0
       <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>
eran otzap
  • 12,293
  • 20
  • 84
  • 139