Home > Net >  Datatemplate Binding in Label-Style
Datatemplate Binding in Label-Style

Time:09-21

I have a class that contains various information like a title, imagesource or a description.

To display these in a listbox I used a datatemplate. This works fine.

<DataTemplate>
 <StackPanel Orientation="Horizontal">
   <Label Content="{Binding Titel}" />
   <Label Content="{Binding Beschreibung}"/>
   <Label >
    <AccessText Text="{Binding Version}"/>
   </Label>
  <Label Style="{DynamicResource ResourceKey={Binding Resource}}"/>
 </StackPanel>
</DataTemplate>

Behind each listbox element I want to display one of three labels. I tried to define the style of each label with the Name of the x:key from the Label. (Here: Labelupdate)

<Label Style="{DynamicResource ResourceKey={Binding Resource}}"/>

Label-Style:

<Style x:Key="Labelupdate" TargetType="Label">
            <Setter Property="HorizontalAlignment" Value="Center"/>
            <Setter Property="VerticalAlignment" Value="Center"/>
            <Setter Property="Content" Value="Update"/>
            <Setter Property="Width" Value="150"/>
            <Setter Property="Height" Value="30"/>
            <Setter Property="FontFamily" Value="{StaticResource Roboto}"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Foreground" Value="{DynamicResource FntBtnColor}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Label}">
                        <Border CornerRadius="2" Background="DeepSkyBlue" >
                            <DockPanel>
                                <Image Source="Images/refresh.png" Height="15" Width="15" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="25 0 0 0"/>
                                <ContentPresenter x:Name="contentPresenterBtn" HorizontalAlignment="Center" VerticalAlignment="Center" Focusable="False" RecognizesAccessKey="True" Margin="0 0 10 0"/>
                            </DockPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Example Code to add the Item to Listbox:

for (int i = 1; i < 6; i  )
{
  items.Add(new ListBxItem("Beschreibung", i   " Titel", "Version", "Labelupdate"));
}
            
InstallListbox.ItemsSource = items;

Unfortunately, this does not work as I want and I have now found no solution after several hours of searching. I am still relatively new in Wpf and C#.

CodePudding user response:

I am afraid you can't bind something to ResourceKey.

Modify the Style or ControlTemplate to include triggers to change properties bases on some property of ListBxItem, e.g.:

<DataTemplate>
...
    <Label>
        <Label.Style>
            <Style TargetType="Label">
                ...
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Resource}" Value="Labelupdate">
                        <Setter Property="Foreground" Value="Blue" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Style>
    </Label>
  • Related