2

我在 WPF 中工作,我正在尝试更改按钮的图像,我创建了一些触发器和设置器,但它不起作用。

这是我的 XAML 代码:

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"  
            Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" >
        <StackPanel>
            <Image>
                <Image.Style>
                    <Style TargetType="Image">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Image.Style>
            </Image>
        </StackPanel>
    </Button>

我无法意识到我做错了什么。希望您能够帮助我。先感谢您。

4

2 回答 2

3

删除. Button._ Path所以而不是

Path=Button.IsEnabled

你应该有

Path=IsEnabled

例子

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"               Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False">
    <StackPanel>
        <Image>
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="True">
                            <Setter Property="Source" Value="Images/MoveFirst_Enabled.bmp" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=topButton, Path=IsEnabled}" Value="False">
                            <Setter Property="Source" Value="Images/MoveFirst_Disabled.bmp" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>
    </StackPanel>
</Button>
于 2012-06-20T16:24:56.630 回答
1

我认为您没有使用正确的 Uri 字符串作为图像的来源。

<Button BorderThickness="0" Height="23" HorizontalAlignment="Left" Margin="59,6,0,0" Name="topButton" Width="26"  VerticalAlignment="Top"  
                Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" IsEnabled="False" >
            <StackPanel>
                <Image>
                    <Image.Style>
                        <Style TargetType="Image">

                                <!-- If you are using the Image as a Resource, you should use the following format.-->

                            <Style.Triggers>    
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                    <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Enabled.bmp" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                    <Setter Property="Image.Source" Value="assemblyname;component/Images/MoveFirst_Disabled.bmp" />
                                </DataTrigger>
                            </Style.Triggers>

                           <!-- If you are using the Image as Content then you should use the below format.-->

                            <Style.Triggers>    
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="True">
                                    <Setter Property="Image.Source" Value="Images/MoveFirst_Enabled.bmp" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding ElementName=topButton, Path=Button.IsEnabled}" Value="False">
                                    <Setter Property="Image.Source" Value="Images/MoveFirst_Disabled.bmp" />
                                </DataTrigger>
                            </Style.Triggers>

                        </Style>
                    </Image.Style>
                </Image>
            </StackPanel>
        </Button>

我希望它可以帮助你...

于 2012-06-20T16:21:13.583 回答