Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Datatemplate Binding in Label-Style

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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#.

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading