What is the XAML syntax for setting a trigger on a shortcut?

I have a DataTemplate that displays objects with three fields, for example:

Name = "Font Color"
Value = "Orange"
Editable = "True"

but I want to display them, for example:

Font Color: Orange Editable

But I can’t find the syntax for using Triggers here to, for example, display “Editable” when the field Editable = "True"

Does anyone know the syntax for this?

The following code causes "Binding cannot be used in Property":

<DataTemplate x:Key="settingsItemTemplate">
    <StackPanel Orientation="Horizontal">
        <Label Content="{Binding XPath=Name}" ContentStringFormat=" {0}:"/>
        <Label Content="{Binding XPath=Value}"/>
        <Label>
            <Label.Triggers>
                <Trigger Property="{Binding XPath=Editable}" Value="True">
                    <Setter Property="Content" Value="Editable"/>
                </Trigger>
                <Trigger Property="{Binding XPath=Editable}" Value="False">
                    <Setter Property="Content" Value="NOT Editable"/>
                </Trigger>
            </Label.Triggers>
        </Label>
    </StackPanel>
</DataTemplate>
+3
source share
1 answer

Will use TextBlockinstead Label? TextBlockhas a property Textthat you should be able to contact in this case.

Label, DataTemplate - , . ContentTemplate .

. , , Trigger Property. DataTrigger :

<StackPanel>
    <CheckBox Name="EditableCheckBox">Is Editable</CheckBox>
    <Label>
        <Label.Resources>
            <Style TargetType="{x:Type Label}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding ElementName=EditableCheckBox, Path=IsChecked}" Value="True">
                        <Setter Property="Content" Value="Editable" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding ElementName=EditableCheckBox, Path=IsChecked}" Value="False">
                        <Setter Property="Content" Value="NOT Editable" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Label.Resources>
    </Label>
</StackPanel>

Binding XML, .

+10

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


All Articles