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

时间:2014-05-21 04:13:57

标签: c# wpf xaml

这是我正在处理的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

1 个答案:

答案 0 :(得分:1)

如果在本地设置属性,则触发器将无法更改precedence引起的值。 将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>
相关问题