WPF改变颜色问题

wozhaozhe2008 2020-06-30 04:03:31
WPF有没有什么好办法改变选中颜色,比方说一个BUTTON按钮,我移入改变颜色,除了Template外还有什么办法吗?
因为用模板的话就什么都需要自己去实现了,玩仅仅只是改变个选中颜色而已。
当然Button按钮没什么,但其他复杂一点的比方说Listview。就仅仅改变个选中颜色值,
接连其他的功能全部都需要自己再去实现一遍
...全文
3309 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
wozhaozhe2008 2020-07-01
  • 打赏
  • 举报
回复
引用 6 楼 icoolno1 的回复:
定义VisualState即可。
我百度搜下,谢谢!
八爻老骥 2020-07-01
  • 打赏
  • 举报
回复
定义VisualState即可。
八爻老骥 2020-07-01
  • 打赏
  • 举报
回复
引用 楼主 wozhaozhe2008的回复:
WPF有没有什么好办法改变选中颜色,比方说一个BUTTON按钮,我移入改变颜色,除了Template外还有什么办法吗?
因为用模板的话就什么都需要自己去实现了,玩仅仅只是改变个选中颜色而已。
当然Button按钮没什么,但其他复杂一点的比方说Listview。就仅仅改变个选中颜色值,
接连其他的功能全部都需要自己再去实现一遍
事件触发器,属性触发器,再不济直接处理事件。
wozhaozhe2008 2020-07-01
  • 打赏
  • 举报
回复
引用 4 楼 独立观察员 的回复:
可以用样式触发器吧
这个是数据触发器:
WPF 让一组 Button 实现 RadioButton 的当前样式效果》(http://dlgcy.com/wpf-button-as-radiobutton/)

你这个要求应该是用事件触发器
好的,我去查下资料看看,谢谢!
wozhaozhe2008 2020-07-01
  • 打赏
  • 举报
回复
引用 9 楼 icoolno1 的回复:
[quote=引用 8 楼 wozhaozhe2008 的回复:][quote=引用 6 楼 icoolno1 的回复:]定义VisualState即可。
网上就找到一个,还直接给了一段代码,连为什么都没说,能简单的说下这怎么用吗?[/quote] 每个控件都有自己的VisualState,可以根据这个东西来定义每个VisualState的外观。比如按钮的VisualState有这些: VisualState Name VisualStateGroup Name Description Normal CommonStates The default state. MouseOver CommonStates The mouse pointer is positioned over the control. Pressed CommonStates The control is pressed. Disabled CommonStates The control is disabled. Focused FocusStates The control has focus. Unfocused FocusStates The control does not have focus. Valid ValidationStates The control uses the Validation class and the HasError attached property is false. InvalidFocused ValidationStates The HasError attached property is true and the control has focus. InvalidUnfocused ValidationStates The HasError attached property is true and the control does not have focus.

<ControlTemplate x:Key="roundbutton" TargetType="Button">
    <Grid>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup Name="CommonStates">
                <VisualState Name="Normal">
                </VisualState>
                <VisualState Name="MouseOver">
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Ellipse x:Name="backgroundElement" Fill="{TemplateBinding Background}" Stroke="{TemplateBinding Foreground}" />
        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
    </Grid>
</ControlTemplate>
[/quote] 一样的,如果不用Template也不会变色,不过还是谢谢了!
独立观察员 2020-07-01
  • 打赏
  • 举报
回复
可以用样式触发器吧
这个是数据触发器:
WPF 让一组 Button 实现 RadioButton 的当前样式效果》(http://dlgcy.com/wpf-button-as-radiobutton/)

你这个要求应该是用事件触发器
八爻老骥 2020-07-01
  • 打赏
  • 举报
回复
引用 8 楼 wozhaozhe2008 的回复:
[quote=引用 6 楼 icoolno1 的回复:]定义VisualState即可。

网上就找到一个,还直接给了一段代码,连为什么都没说,能简单的说下这怎么用吗?[/quote]

每个控件都有自己的VisualState,可以根据这个东西来定义每个VisualState的外观。比如按钮的VisualState有这些:

VisualState Name VisualStateGroup Name Description
Normal CommonStates The default state.
MouseOver CommonStates The mouse pointer is positioned over the control.
Pressed CommonStates The control is pressed.
Disabled CommonStates The control is disabled.
Focused FocusStates The control has focus.
Unfocused FocusStates The control does not have focus.
Valid ValidationStates The control uses the Validation class and the HasError attached property is false.
InvalidFocused ValidationStates The HasError attached property is true and the control has focus.
InvalidUnfocused ValidationStates The HasError attached property is true and the control does not have focus.


<ControlTemplate x:Key="roundbutton" TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal">
</VisualState>
<VisualState Name="MouseOver">
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Ellipse x:Name="backgroundElement" Fill="{TemplateBinding Background}" Stroke="{TemplateBinding Foreground}" />
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
wozhaozhe2008 2020-07-01
  • 打赏
  • 举报
回复
引用 6 楼 icoolno1 的回复:
定义VisualState即可。
网上就找到一个,还直接给了一段代码,连为什么都没说,能简单的说下这怎么用吗?
wozhaozhe2008 2020-06-30
  • 打赏
  • 举报
回复
引用 1 楼 以专业开发人员为伍 的回复:
ListView的“选中颜色值”是指什么值?Button的“选中颜色值”呢?其它控件呢?请贴出4、5个例子来了解一下问题。
还是拿button来举例,我用template改变了背景色,那么我就需要去实现按下的颜色,当然button简单,只需要多加一个条件去实现按下背景色,但listview就要实现太多的功能了。
wozhaozhe2008 2020-06-30
  • 打赏
  • 举报
回复
引用 1 楼 以专业开发人员为伍 的回复:
ListView的“选中颜色值”是指什么值?Button的“选中颜色值”呢?其它控件呢?请贴出4、5个例子来了解一下问题。
就是鼠标移入button控件,改变button背景颜色,用style是没效果的,只能用template。同理,listview也一样。
  • 打赏
  • 举报
回复
ListView的“选中颜色值”是指什么值?Button的“选中颜色值”呢?其它控件呢?请贴出4、5个例子来了解一下问题。

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧