我不能使用样式标签(XAML)在按钮之间切换

本文关键字:按钮 之间 XAML 不能 样式 标签 | 更新日期: 2023-09-27 18:04:25

这是我正在编写的xaml/c#合同的代码(我是一个新程序员,但我花了很多时间来验证这一点)。我已经搜索了SO的帮助,并最终与以下代码,这应该工作和编译良好,但我的按钮仍然没有改变为禁用的艺术资产,当他们的IsEnabled设置为False。

这是我的代码

<Window x:Class="My_Project.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MyProject" Height="654" Width="943">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Rectangle Grid.Row="0" Grid.Column="1" Fill="#58585A" Margin="0.4,0,-0.2,-0.2"/>
        <StackPanel Grid.Row="0" Grid.Column="0" VerticalAlignment="Top">
            <Image Source="img'company_logo_full.png" Width="200" HorizontalAlignment="Left" Margin="20,0,0,0"></Image>
            <Separator Height="35" Margin="20,0,20,0"/>
            <Button x:Name="NewButton" Click="NewButton_Click" Margin="20,0,0,0" Width="208" Height="35" HorizontalAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}" >
                <Image Source="img'new_button.png"  Width="208" Height="35"></Image>
            </Button>
            <Button x:Name="OpenButton" Click="OpenButton_Click" Margin="20,0,0,0" Width="208" Height="35" HorizontalAlignment="Left" BorderBrush="{x:Null}" Foreground="{x:Null}" Background="{x:Null}" >
                <Image Source="img'open_button.png"  Width="208" Height="35">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
                                    <Setter Property="Source" Value="img'open_button.png" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
                                    <Setter Property="Source" Value="img'open_button_disabled.png" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
            </Button

非常感谢你的时间,~ QP

我不能使用样式标签(XAML)在按钮之间切换

如果您在本地设置属性,则由于优先级的关系,触发器将无法更改值。将Source属性移动到样式中:试试这个

 <Image Width="208" Height="35">
                        <Image.Style>
                            <Style TargetType="Image">
 <Setter Property="Source" Value="img'open_button.png" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="True">
                                        <Setter Property="Source" Value="img'open_button.png" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Button}}" Value="False">
                                        <Setter Property="Source" Value="img'open_button_disabled.png" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Image.Style>
                    </Image>