【问题标题】:Change button background WPF mouseover property with c# conditions使用 c# 条件更改按钮背景 WPF 鼠标悬停属性
【发布时间】:2015-05-22 10:00:55
【问题描述】:

我有一个大问题,我不知道该怎么办。我正在 c# WPF 上编写一个客户端。我的表单上有 3 个元素(文本框、密码框、按钮)。因此,当人们填写文本框和密码框文本时,我想将按钮背景更改为“活动(更改图像)”。诸如登录名和密码之类的东西。

 private void eventhandler(object sender, RoutedEventArgs e)
    {
        if (login.Text.Length > 0 && password.Password.Length > 0)
        {

            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                 new BitmapImage(new Uri("pack://application:,,,/Images/Enter Enabled.bmp", UriKind.Absolute));
            Enter.Background = myBrush;

           }
        else if (login.Text.Length < 1 || password.Password.Length < 1)
        {

            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                 new BitmapImage(new Uri("pack://application:,,,/Images/Enter Disabled.bmp", UriKind.Absolute));
            Enter.Background = myBrush;
        }
    }

所以我有这个 c# 代码。它有效,但现在我有一个问题。当我将鼠标悬停在按钮上时,它的背景会变回“已禁用”。

Xaml

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="250" Width="288" Background="#FF494949">
<Window.Resources>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle">
            <Setter.Value>
                <Style>
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="#FFDDDDDD"/>
        <Setter Property="BorderBrush" Value="#FF707070"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <ImageBrush ImageSource="Images/Enter Pressed.bmp"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid Margin="0,0,2,-4.478">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Button HorizontalAlignment="Left" Name="Enter" Margin="46,92,0,0" VerticalAlignment="Top" Width="127" Height="27" Grid.Column="1" BorderThickness="0" Style="{DynamicResource ButtonStyle1}">
        <Button.Background>
            <ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
        </Button.Background>
    </Button>
    <TextBox Grid.ColumnSpan="2" Name="login" HorizontalAlignment="Left" Height="23" Margin="53,23,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120" TextChanged="eventhandler"/>
    <PasswordBox Grid.ColumnSpan="2" Name="password" HorizontalAlignment="Left" Margin="53,59,0,0" VerticalAlignment="Top" Width="120" PasswordChanged="eventhandler"/>

</Grid>

我知道这是因为鼠标悬停图像刷,但我如何锁定它或在上述条件下更改 c# 代码。 Testapplication

我已经在 takebin 上上传了 testapp。因此,只需用文本填充文本框和密码框,然后将鼠标悬停在按钮上,您就会看到问题所在。 谢谢大家的回复和帮助

【问题讨论】:

    标签: c# wpf button background styles


    【解决方案1】:

    同意。如果您不想要这样的行为,请删除它:

       <Trigger Property="IsMouseOver" Value="True">
           <Setter Property="Background" TargetName="border">
                <Setter.Value>
                   <ImageBrush ImageSource="Images/Enter Disabled.bmp"/>
                </Setter.Value>
             </Setter>
        <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/>
      </Trigger>
    

    【讨论】:

      【解决方案2】:

      WPF 按钮已包含此功能。 您可以将按钮的 IsEnabled 属性绑定到视图模型中的布尔值,然后使用视觉状态在样式 xml 中定义启用、禁用和移动的样式。

      您可以在此处找到默认 WPF 按钮样式的源代码: https://msdn.microsoft.com/en-us/library/ms753328(v=vs.110).aspx

      然后你也可以根据你的文本框的属性变化来改变绑定到 IsEnabled 的属性。

      【讨论】:

      • 哦,顺便说一句,如果您使用响应式扩展,您可以在命令中直接定义启用/禁用功能,如下所示:this.SelectAll = new DelegateCommand(() =&gt; this.ApplySelection(this.resultsDataGridViewModel, true), this.CanApplySelection); 其中SelectAll 绑定到按钮的命令
      猜你喜欢
      • 1970-01-01
      • 2017-07-28
      • 2015-01-14
      • 1970-01-01
      • 2017-12-15
      • 2011-04-04
      • 1970-01-01
      • 2019-02-05
      • 2014-10-16
      相关资源
      最近更新 更多