Applying a DataTrigger to a button whose binding comes from another control?

I am trying to apply DataTrigger

to Button

and depends on a property from the currently selected item TreeView

. The idea is that I want to change the text Button

based on the property of the selected item.

What do I see:

<Button x:Name="m_AddObject" Margin="192.708,0.909,6,6.363" Click="AddObject_Click" >
    <DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Removable}" Value="true">
        <Setter TargetName="m_AddObject" Property="Content" Value="Remove" />
    </DataTrigger>
</Button>

      

But I cannot compile it. The setter complains that "Content" is invalid because it has no qualifying type name, but if I change it to "Button.Content" it complains about "Object reference not set to an object instance".

I've also tried:

<Setter TargetName="m_AddObject.Content" Value="Remove" />

      

While it compiles it doesn't work either.

I'm stumped.

Any ideas?

Thanks!

0


a source to share


1 answer


DataTriggers should be styled for the button. What you are trying above is basically using DataTriggers as the "label" ("Content" as WPF does) for the button (instead of, say, "OK").

It's ad-hoc, so it might not be entirely correct, but it's closer to what you want:



<Button x:Name="m_AddObject"
        Margin="192.708,0.909,6,6.363"
        Click="AddObject_Click">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=ObjectTreeView, Path=SelectedItem.Removable}" Value="True">
                    <Setter Property="Content" Value="Remove" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

      

+1


a source







All Articles