This question has already been answered:

There is a counter that needs to be used in two versions:

  • horizontal arrangement of elements
  • vertical arrangement of elements

For this, I created the BytePropertyView.xaml myself:

<Border Style="{StaticResource MainBorder}"> <WrapPanel Orientation="Vertical"> <TextBlock Grid.Row="0" Style="{StaticResource MainName}" Text="{Binding Name}"/> <ItemsControl Grid.Row="1" ItemsSource="{Binding Properties}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> !!!Сюда нужно подписаться!!! <WrapPanel/> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Name}"/> <CheckBox IsChecked="{Binding IsChecked}"/> </StackPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </WrapPanel> </Border> 

But its .cs code (this is not VM, but the code that the IDE itself generates and I added DP to it) BytePropertyView.xaml.cs:

 public partial class BytePropertyView : UserControl { public BytePropertyView() { InitializeComponent(); } public Orientation ItemsOrientation { get { return (Orientation)GetValue(ItemsOrientationProperty); } set { SetValue(ItemsOrientationProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty ItemsOrientationProperty = DependencyProperty.Register("ItemsOrientation", typeof(Orientation), typeof(BytePropertyView), new PropertyMetadata(Orientation.Horizontal)); } 

I need to sign an Orientation in ItemsControl on the ItemsOrientation property. Or how to add a custom property for the control?

Reported as a duplicate by Andrey NOP c # 10 Jul '18 at 15:15 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    Yeah, now it’s clear, for now without a PC, so I’ll write in the comments: give your UsrControl a name in the markup and attach it: {Binding ItemsOrientation, ElementName=name} - Andrew NOP
  • You can still through RelativeSource FindAncestor, but it will be more cumbersome - Andrew NOP
  • @ AndreiNOP thank you very much! I hope someday I can reach your level of knowledge! - UporotayaPanda pm
  • If everything worked out - write the answer - Andrey NOP
  • @ AndreiNOP is ready! Thank! - UporotayaPanda

1 answer 1

For binding, you must assign the name UserControl :

 <UserControl x:Class="GraceConfig.View.BytePropertyView" ... Name="ByteProperty"> 

And where you need to subscribe, specify the name of the property and the name of the UserControl :

 <WrapPanel Orientation="{Binding ItemsOrientation, ElementName=ByteProperty}"/> 

When using, set the property:

 <local:BytePropertyView ItemsOrientation="Vertical"/>