Set Usercontrol as a data table

I have the following ProductList template snipet template

<Style x:Key="ProductListStyle" TargetType="{x:Type s:SurfaceListBox }"> <Setter Property="Background" Value="{DynamicResource {x:Static s:SurfaceColors.ListBoxItemBackgroundBrushKey}}" /> <Setter Property="SelectionMode" Value="Single" /> <Setter Property="Height" Value="234" /> <Setter Property="ItemTemplateSelector"> <Setter.Value> <sc:ProductListTemplateSelector> <sc:ProductListTemplateSelector.NormalItemTemplate> <DataTemplate> <StackPanel RenderTransformOrigin="0.5, 0.5" Margin="7,0,0,0" MinWidth="171" MaxWidth="171" MinHeight="235" MaxHeight="235"> <Image Margin="14,21,21,11" Source="{Binding XPath=@Image }" Height="149" Width="101" /> <TextBlock Text="{Binding XPath=@Name }" MaxWidth="116" FontSize="12" Margin="21,0,21,21" FontFamily="Segoe360" TextAlignment="Center" TextWrapping="Wrap" Foreground="{DynamicResource {x:Static s:SurfaceColors.ListBoxItemForegroundBrushKey}}" HorizontalAlignment="Center" /> </StackPanel> </DataTemplate> </sc:ProductListTemplateSelector.NormalItemTemplate> 

I need to replace the DataTemplate with this style, which will be set using my custom control, for example

 <local:MyUserControl> 

Keeping only the section, I did not get my control when my Itemource is installed with the myUserControl collection

+2
source share
1 answer

Usually I just add a DataTemplate to Resources . This can be <Window.Resources> or <App.Resources> if the data template is global, or FrameworkElement.Resources if the template should be used only in the specified area. For example, adding a template to ListView.Resources will only apply the template to a specific ListView.

 <Window.Resources> <DataTemplate DataType="{x:Type local:ProductModel}"> <local:MyUserControl /> </DataTemplate> </Window.Resources> 

As a side note, your original question leads me to believe that you are binding a ListView to a collection of MyUserControl objects. I really would not recommend this, but if it is, you can use the ContentControl in your DataTemplate, and the Content bound to your object and it should display correctly.

 <ContentControl Content="{Binding }" /> 
+1
source

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


All Articles