WPF RelativeSource问题与嵌套控件

时间:2022-09-17 14:27:15

I have the following markup:

我有以下标记:

<Button Name="m_SaveButton" Command="{Binding SaveCommand}">
    <StackPanel>
        <Image Source="{StaticResource IconSave16}">
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}}" Value="False">
                            <Setter Property="Source" Value="{StaticResource IconSaveInactive16}" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
        <Label Content="Save" />
    </StackPanel>
</Button>

I want to change the Image nested inside the Button when Button.IsEnabled is false. The markup above is not working.

当Button.IsEnabled为false时,我想更改嵌套在Button内的Image。上面的标记不起作用。

I was trying to use Meleak's code found here: WPF Mouseover Trigger Effect for Child Controls

我试图使用这里找到的Meleak代码:用于儿童控制的WPF鼠标悬停触发效果

Does anyone can suggest me a solution for this?

有人能建议我解决这个问题吗?

Thank you in advance!

先谢谢你!

1 个解决方案

#1


0  

You can't change a local value in style because of Value Precedence. This should work.

由于Value Precedence,您无法更改样式中的本地值。这应该工作。

<Image>
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Source" Value="{StaticResource IconSave16}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}}" Value="False">
                    <Setter Property="Source" Value="{StaticResource IconSaveInactive16}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

#1


0  

You can't change a local value in style because of Value Precedence. This should work.

由于Value Precedence,您无法更改样式中的本地值。这应该工作。

<Image>
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Source" Value="{StaticResource IconSave16}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}}" Value="False">
                    <Setter Property="Source" Value="{StaticResource IconSaveInactive16}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>