WPF: problem with DataControl datatemplate as user control

This works great:

 <ItemsControl ItemsSource="{Binding Persons}">
   <ItemsControl.ItemsPanel>
     <ItemsPanelTemplate>
       <StackPanel Orientation="Vertical" />
     </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
     <DataTemplate>
       <StackPanel Orientation="Horizontal">
         <TextBlock Text="{Binding Path=FirstName}"/>
         <TextBlock Text="{Binding Path=LastName}"/>
       </StackPanel>
     </DataTemplate>
   </ItemsControl.ItemTemplate>
 </ItemsControl>

But I want to use User Control instead of the stack panel with two TextBlocks, using something like:

  <DataTemplate>
    <jasControls:NameView/>
  </DataTemplate>

But with this and a million other syntaxes, NameView does not show anything, but it works fine in a separate test outside the ItemsControl element with an explicitly set property (dependency) - for example.

  <jasControls:NameView FName="{Binding Path=Person.FirstName}"  />

, , "" ()? ? ? ItemsControl , . ListBox? , Person? ? , ?! - , .

- TIA

+3
1

, Persons Person, DataContext NameView Person.

. NameView :

<UserControl ...>
   <StackPanel Orientation="Horizontal">
      <TextBlock Text="{Binding FirstName}"/>
      <TextBlock Text="{Binding LastName}"/>
   </StackPanel>
</UserControl>

, .

Person DataContext:

Person person = this.DataContext as Person;
+5

Source: https://habr.com/ru/post/1717403/


All Articles