tags:

views:

770

answers:

1

I have a DataTemplate that is displaying objects with three fields, e.g.:

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

but I want to display them as e.g.:

Font Color: Orange Editable

But I'm having trouble finding the syntax to use Triggers here in order to e.g. display "Editable" when the field Editable="True"

Does anyone know the syntax to do this?

The following code results in "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>
+1  A: 

Would it work to use a TextBlock instead of a Label? TextBlock does have a Text property that you should be able to bind to in this case.

If you really want to use a Label, another approach would be to create two DataTemplate's - one for the editable case, and another for non-editable. You can then bind the ContentTemplate property to the appropriate template.

Update: After looking into it some more, it looks like Trigger does not support binding for its Property attribute. However, DataTrigger does support this:

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

You should be able to modify the Binding attribute to bind to your XML data source instead of do the value of another control.

Andy
I tried that but still get the error that I can't have the binding in the property. I don't see how to tell it in my trigger to: look at the value of the "Editable" property in the current object to see if it is "True", lost in the syntax jungle here.
Edward Tanguay