Skip to main content
 首页 » 编程设计

c#之具有绑定(bind)图像状态和悬停图像的 WPF 无 Chrome 按钮

2024年02月23日36kenshinobiy

我在使用以下 XAML 样式的 WPF 应用程序中有一个无边框按钮: 但是,我想用它做两件事。

  1. 我希望显示的图像是两个图像之一,具体取决于按钮所在模板的数据上下文中的绑定(bind)属性。该属性是一个 bool 值。如果为假,则显示一张图片,如果为真,则显示另一张。
  2. 我希望显示的图像在鼠标悬停在按钮上时发生变化,并成为当上述绑定(bind)属性为 True 时显示的图像。

我已经尝试了几种方法,但似乎没有任何效果。我尝试了一个带有这样绑定(bind)值的转换器:

        <x:ArrayExtension x:Key="ThumbsDown" Type="BitmapImage"> 
            <BitmapImage UriSource="/Elpis;component/Images/thumbDown.png" /> 
            <BitmapImage UriSource="/Elpis;component/Images/thumbBan.png" /> 
        </x:ArrayExtension> 
 
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            var data = (object[])parameter; 
 
            if (data.Length != 2 || value == null || value.GetType() != typeof(bool)) 
                return data[0]; 
 
            if(!((bool)value)) 
                return data[0]; 
            else 
                return data[1]; 
        } 
 
<Button Name="btnThumbDown" Grid.Column="1" Style="{StaticResource NoChromeButton}"  
                                            VerticalAlignment="Center"  
                                            HorizontalAlignment="Center"  
                                            Background="Transparent" 
                                            Click="btnThumbDown_Click"> 
                                <Image  Width="32" Height="32" Margin="2"  
                                        Source="{Binding Banned,  
                                                Converter={StaticResource binaryChooser},  
                                                ConverterParameter={StaticResource ThumbsDown}}"/> 
                            </Button> 

但这会导致两个问题。我为悬停图像做的任何事情都不再有效,设计师抛出异常。

下面的不透明度触发器可以触发,因为我现在真的只想要悬停。

关于如何使其正常工作的任何想法?

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style x:Key="NoChromeButton" TargetType="{x:Type ButtonBase}"> 
        <Setter Property="Background" Value="Transparent"/> 
        <Setter Property="BorderThickness" Value="1"/> 
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
        <Setter Property="HorizontalContentAlignment" Value="Center"/> 
        <Setter Property="VerticalContentAlignment" Value="Center"/> 
        <Setter Property="Padding" Value="1"/> 
        <Setter Property="Template"> 
            <Setter.Value> 
                <ControlTemplate TargetType="{x:Type ButtonBase}"> 
                    <Grid x:Name="Chrome" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  
                                          Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"  
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"  
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
                    </Grid> 
                    <ControlTemplate.Triggers> 
                        <Trigger Property="IsMouseOver" Value="True"> 
                            <Setter Property="Opacity" Value="0.75"/> 
                        </Trigger> 
                        <Trigger Property="IsMouseOver" Value="False"> 
                            <Setter Property="Opacity" Value="1.0"/> 
                        </Trigger> 
                    </ControlTemplate.Triggers> 
                </ControlTemplate> 
            </Setter.Value> 
        </Setter> 
    </Style> 
</ResourceDictionary> 

请您参考如下方法:

你将不得不做几件事:

  1. 为每个要显示的图像设置 ImageBrush 资源。
  2. 为 Button 创建一个 ControlTemplate,它具有内置 Style,鼠标悬停在 Trigger 上。
  3. 通过将 Background 属性绑定(bind)到第 1 点中的 ImageBrush 资源之一,在 Button 上设置图像。
  4. 使用鼠标悬停 Trigger 将 Background 属性更改为其他 ImageBrush 资源。

请参阅下面的代码作为一个简单示例(您必须使用自己的图像来代替“向上”和“向下”)。

    <Window x:Class="ButtonHover.MainWindow" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
            xmlns:my="clr-namespace:ButtonHover" 
            Title="MainWindow" Height="227" Width="280"> 
        <Window.Resources> 
            <ImageBrush x:Key="imageABrush" 
                        ImageSource="/ButtonHover;component/Resources/up.png"/> 
            <ImageBrush x:Key="imageBBrush" 
                        ImageSource="/ButtonHover;component/Resources/down.png"/> 
            <ControlTemplate x:Key="buttonTemplate" TargetType="Button"> 
                <Border BorderThickness="3" BorderBrush="Black"> 
                    <Border.Style> 
                        <Style> 
                            <Style.Setters> 
                                <Setter Property="Border.Background" 
                                        Value="{StaticResource imageABrush}"/> 
                            </Style.Setters> 
                            <Style.Triggers> 
                                <Trigger Property="Button.IsMouseOver" Value="True"> 
                                    <Setter Property="Border.Background" 
                                            Value="{StaticResource imageBBrush}"/> 
                                </Trigger> 
                            </Style.Triggers> 
                        </Style> 
                    </Border.Style> 
                </Border> 
            </ControlTemplate> 
        </Window.Resources> 
        <Grid> 
            <Button Height="75" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center" 
                    Width="75" 
                    Template="{StaticResource buttonTemplate}" /> 
        </Grid> 
    </Window> 

这里的关键是在 Border 的 Style 上实现触发器,而不是直接在 Button 上实现。当尝试从应用于 Button 的 Style 设置 Border 的属性时,我无法让它工作。您还可以将 Border 替换为 Rectangle 或将任何其他 ContentControl 替换为 BackGround 属性。

从这里,根据您的应用进行定制。