Use a property trigger to change a property that already has a binding.

How can I use a property trigger in a style (or other method) to change a property (e.g. ToolTip) that already has a value defined by the binding?

I have a simple button:

<Button Name="Button1" Style="{StaticResource ButtonStyle}" 
        ToolTip="{Binding Name}" >My Button</Button>

It has a tooltip binding to show the Name property for a set of classes as a DataContext.

My problem is that I want to show the name when the button is on, but something else when it is disabled. I thought I could get around my problem with style and trigger:

<Style TargetType="Button" x:Key="ButtonStyle">
    <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Property="ToolTip" Value="Disabled" />
        </Trigger>
    </Style.Triggers>
</Style>

But that does not work. If I removed the tooltip binding from the button, then I will get the correct tooltip when the button is disabled. But it seems that I cannot have both a binding and a trigger on the same property.

I could get around this by adding another trigger:

<Trigger Property="IsEnabled" Value="true">
    <Setter Property="ToolTip" Value="{Binding Name}" />
</Trigger>

4 5 , ToolTip, () , .

?

+3
2

- , (_DisabledButtonToolTipStyle ), , . , , ; , , .

<Window.Resources>
    <Style x:Key="_DisabledButtonToolTipStyle" TargetType="Button">
        <Setter Property="ToolTipService.ShowOnDisabled" Value="True" />
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="ToolTip" Value="Disabled" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <Button Name="Button1" Content="My Button">
        <Button.Style>
            <Style TargetType="Button" BasedOn="{StaticResource _DisabledButtonToolTipStyle}">
                <Setter Property="ToolTip" Value="{Binding Name}" />
            </Style>
        </Button.Style>
    </Button>
</Grid>
+3

, , , , , , :

<Style TargetType="Button">
    <Setter Property="Background" Value="Blue"/>
</Style>

:

<Button Content="1st"/>
<Button Content="2nd" Background="Red"/>

, .

, , , , DataTemplate, (TextBox CheckBox ):

<ContentPresenter Content="{Binding}">
    <ContentPresenter.ContentTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBox Name="edt" Text="Tooltip text"/>
                <Button Name="btn" Content="x" 
                    ToolTip="{Binding ElementName=edt, Path=Text}" 
                    ToolTipService.ShowOnDisabled="True"/>
                <CheckBox Content="Enabled" 
                    IsChecked="{Binding ElementName=btn, Path=IsEnabled}"/>
            </StackPanel>
            <DataTemplate.Triggers>
                <Trigger SourceName="btn" Property="IsEnabled" Value="False">
                    <Setter TargetName="btn" Property="ToolTip" Value="Disabled"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ContentPresenter.ContentTemplate>
</ContentPresenter>
+2

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


All Articles