Data binding data template with a user control
Posted: 30 Aug 2022, 21:05
I have a data binding question. I have an ItemsControl that is bound to an observable collection of items. My data template has been working fine, but it started to get really big.. So I decided to try to move the contents of the data template into a user control.
Unfortunately, this seems like it breaks all the data bindings. For some reason, the data bindings only work if they are directly inside the data template.
Is there a way to fix this?
thanks,
sam
Unfortunately, this seems like it breaks all the data bindings. For some reason, the data bindings only work if they are directly inside the data template.
Code: Select all
<UserControl.Resources>
<ResourceDictionary>
<DataTemplate DataType="{x:Type local:TeamInventoryItem}">
<StackPanel>
<!-- this doesn't work - the view shows up but all the data bindings are broken -->
<local:TeamInventoryItemView DataContext="{Binding}" />
<!-- this works -->
<TextBlock x:Name="KeyBinding" Text="{Binding Name}" FontFamily="{StaticResource Font.Family.LeMondeSans}" FontWeight="Bold" FontSize="12" Foreground="#FF00FF" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Bottom" Padding="4,1"/>
</StackPanel>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
thanks,
sam