C# 在WPF中鼠标悬停时,如何更改组合框边框的颜色?

C# 在WPF中鼠标悬停时,如何更改组合框边框的颜色?,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,昨天我写了类似的关于textbox的帖子,也是出于同样的目的。我还告诉过你我对combobox有问题。我已经得到了有用的答案,基本上解决了我的文本框问题。我试图为我的组合框应用类似的解决方案,但没有成功。然后我试图在网络上找到一些解决方案,包括stack和msdn以及其他网站,但没有成功。然后我从昨天开始编辑我的帖子,所以有人建议我提出新问题,所以我正在做 所以我的问题是,当鼠标在组合框上时,我只想改变边框的颜色。剩下的功能和设计应该保留在标准组合框中 我有这样一个代码,用于按钮,并修改文本框,

昨天我写了类似的关于textbox的帖子,也是出于同样的目的。我还告诉过你我对combobox有问题。我已经得到了有用的答案,基本上解决了我的文本框问题。我试图为我的组合框应用类似的解决方案,但没有成功。然后我试图在网络上找到一些解决方案,包括stack和msdn以及其他网站,但没有成功。然后我从昨天开始编辑我的帖子,所以有人建议我提出新问题,所以我正在做

所以我的问题是,当鼠标在组合框上时,我只想改变边框的颜色。剩下的功能和设计应该保留在标准组合框中

我有这样一个代码,用于按钮,并修改文本框,但我不能将其应用于组合框。 此代码位于应用程序资源中的app.xaml中

    <Style TargetType="ComboBox">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Foreground" Value="#000000"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Border Name="combobox" 
                        CornerRadius="2"
                        BorderThickness="2"
                        BorderBrush="{TemplateBinding BorderBrush}"  
                        Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="combobox" Property="BorderBrush" Value="#FF0000" />
                            <Setter Property="Foreground" Value="#FF0000"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我刚才粘贴的代码正在创建边框颜色更改,但是combobox的内容正在消失,实际上combobox的所有功能都消失了

我如何在我的应用程序中完成最后一件事?希望有人告诉我这次该做些什么来让它生效

提前谢谢

p、 美国。 有人可以为初学者推荐值得信赖的xaml循序渐进的课程吗?xaml和wpf中的这些技术对Windows通用应用程序有用吗

!!!!! 编辑:

你可能是对的,因为我没有那么丰富的经验,我可能会在xaml中犯错误

我现在所做的也是第三种方式:

1) 加上


(二)



我做错了什么?我在网上找到了这种方法。

通过替换组合框的模板,您已经完全替换了控件的所有可视元素。通过将触发器添加到样式中并保持原始模板不变,您可以实现想要的效果,而不是重新设置模板:

<Style TargetType="ComboBox">
    <Setter Property="Foreground" Value="#000000"/>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="BorderBrush" Value="#FF0000" />
            <Setter Property="Foreground" Value="#FF0000"/>
        </Trigger>
    </Style.Triggers>
</Style>


如果要重新设置控件模板,可以在MSDN上找到完整的可自定义模板,这些模板应仍然支持所有标准功能。

因此,如果要查看默认的
组合框
模板(右键单击->编辑模板->编辑副本),将有一组笔刷列为参考资料。其中之一是,

<SolidColorBrush x:Key="ComboBox.MouseOver.Border" Color="#FF7EB4EA"/>
注意这里设置的
ComboBoxToggleButton
样式?这是下一条线索。如果我们检查特定样式的触发器,更具体地说是针对
IsMouseOver
使用我们首先找到的
Brush
资源查看它

<Setter Property="BorderBrush" TargetName="templateRoot" 
        Value="{StaticResource ComboBox.MouseOver.Border}"/>

我认为触发器不起作用,因为您正在修改的
BorderBrush
与鼠标悬停在控件上时使用的
BorderBrush
不同。我相信您必须编辑现有的默认样式才能获得所需的功能

对于WPF,转到designer并右键单击组合框->编辑模板->编辑副本

然后更改以下内容:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
        <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ComboBox}}}" Value="false"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" TargetName="templateRoot">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFECF4FC" Offset="0"/>
                <GradientStop Color="#FFDCECFC" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" TargetName="templateRoot" Value="Red"/>
</MultiDataTrigger>
<VisualState x:Name="PointerOver">
<Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>
对于通用应用程序,转到designer并右键单击组合框->编辑模板->编辑副本

然后更改以下内容:

<MultiDataTrigger>
    <MultiDataTrigger.Conditions>
        <Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true"/>
        <Condition Binding="{Binding IsEditable, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ComboBox}}}" Value="false"/>
    </MultiDataTrigger.Conditions>
    <Setter Property="Background" TargetName="templateRoot">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                <GradientStop Color="#FFECF4FC" Offset="0"/>
                <GradientStop Color="#FFDCECFC" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>
    <Setter Property="BorderBrush" TargetName="templateRoot" Value="Red"/>
</MultiDataTrigger>
<VisualState x:Name="PointerOver">
<Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>

希望这能满足您的需要。

在要使用它的XAML页面或App.XAML中使用以下代码

<Style x:Key="UpdateComboBoxStyle" TargetType="{x:Type ComboBox}" 
   BasedOn="{StaticResource {x:Type ComboBox}}">
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="BorderBrush" Value="#FF0000" />
        <Setter Property="Foreground" Value="#FF0000"/>
    </Trigger>
  </Style.Triggers>
</Style>

如果不起作用,请告诉我。在这种情况下,只要向我们展示新代码,我们就可以找出哪里出了问题。尝试编辑问题,而不是评论,以便其他人可以了解新的更改。

感谢您的时间和回答。不幸的是,这并不能解决我的问题。我已经将它添加到app.xaml的应用程序资源中,然后组合框就消失了。另一方面,如果我尝试将其放入其中,则在我尝试进入包含此组合框的窗口时,会发生以下错误。PresentationFramework.dll中发生类型为“System.InvalidOperationException”的未处理异常。其他信息:在使用ItemsSource之前,Items集合必须为空。答案中的样式正确,您似乎不知道如何使用样式。另外,请删除OverridesDefaultStyle setter。我确信您只是没有先查看实际模板,或者先测试您的答案,但是在更改
IsMouseOver
的情况下,您的答案是不正确的。所以没有理由批评OP。他的答案是正确的,唯一的问题是他没有继承默认的风格。他只需要补充一下“BasedOn”属性。你找到你的解决方案了吗?嗨,Crhis和其他人。昨天我回到这个项目,我已经完成了它。实际上,我无法使用您的代码,出现了一些缺少参考或其他问题,Visual Studio没有识别出一些东西。但在你的提议背后,我自己尝试了将模板复制到应用程序资源,在那里我尝试了更改所有负责颜色的属性,最后我或多或少地按照我的意愿定制了组合框,只是箭头消失了,但这没什么大不了的。非常感谢。我对你的答案投了赞成票!祝大家今天过得愉快!有时候会有细微差别,所以我更喜欢使用直接从项目中复制的模板,而不是从“默认”文档中复制的模板。很高兴你找到了解决办法!
<Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF569DE5"/>
<VisualState x:Name="PointerOver">
<Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlPageBackgroundAltMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="Background">
        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}"/>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>
<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumBrush}"/>
<Style x:Key="UpdateComboBoxStyle" TargetType="{x:Type ComboBox}" 
   BasedOn="{StaticResource {x:Type ComboBox}}">
  <Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="BorderBrush" Value="#FF0000" />
        <Setter Property="Foreground" Value="#FF0000"/>
    </Trigger>
  </Style.Triggers>
</Style>
Style="{StaticResource UpdateComboBoxStyle}"