更改按钮图像问题

时间:2012-06-20 15:59:12

标签: wpf image xaml controls

我在WPF工作,我正在尝试更改按钮的图像,我已经创建了一些触发器和Setter,但它无法正常工作。

这是我的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>

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

2 个答案:

答案 0 :(得分:3)

删除Button.的{​​{1}}。而不是

Path
你应该

Path=Button.IsEnabled

实施例

Path=IsEnabled

答案 1 :(得分: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>

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