在wpf中选择控件的样式 [英] Select style for control in wpf

查看:126
本文介绍了在wpf中选择控件的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我决定听取sergey建议并将我的项目从winform应用程序转换为wpf。现在我在尝试处理控件的样式时遇到了麻烦。问题是这样的:

我的窗口中有2个数据网格。一个用于收入来源(绿色),一个用于结果来源(红色)。该项目属于金融领域。 aniway,我设法将网格的样式连接到数据网格。现在,我需要有2个样式,每个数据网格一个(因为我需要收入网格将颜色变为绿色,而悬停和结果网格变为红色,因此对于其他事件)。

i需要知道,在我有2个样式名为DgOutcomeStyle和DgIncomeStyle的情况下,如何使样式仅针对一个数据网格,因为现在样式对每个数据都有影响我创造。

希望我已经足够清楚。

谢谢,

oron。

< datagrid x :name =DgIncomexmlns:x =#unknown>

Style ={x:Null}

Horizo​​ntalAlignment =Left

VerticalAlignment =Top

身高=669宽度=150

删除={x:Null}

BorderThickness =0

HeadersVisibility =NoneMargin =400,10,0,0BorderBrush ={x:Null}Grid.Column =4GridLinesVisibility =无FontWeight =SemiBoldFontSize =18

>



< datagrid x:name =DgOutcomexmlns: x =#unknown>

Horizo​​ntalAlignment =Left

VerticalAlignment =Top

身高=669宽度=150

删除={x: Null}

BorderThickness =0

HeadersVisibility =NoneBorderBrush ={x:Null}Margin =10,10,0,0GridLinesVisibility =无FontWeight =SemiBoldFontSize =18

>

hello!
i decided to listen to sergey advice and convert my project from winform application to wpf. now i'm having trouble trying to handle the style for my controls. the problem goes like this:
i have 2 datagrid in my window. one for income sources (green) and one for outcome sources (red color). the project is in the finance field. aniway, i manage to connect the style of the grid to the datagrid. now, i need to have 2 styles, one for each datagrid (because i need the income grid will change the color to green while hover and the outcome grid to red and so for other events).
i need to know, in a situation when i have 2 style called "DgOutcomeStyle" and "DgIncomeStyle", how to make a style goes specific to only one datagrid because right now the style is effect every datagris i create.
hope i'v been clear enough.
thanks,
oron.
<datagrid x:name="DgIncome" xmlns:x="#unknown">
Style="{x:Null}"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="669" Width="150"
removed="{x:Null}"
BorderThickness="0"
HeadersVisibility="None" Margin="400,10,0,0" BorderBrush="{x:Null}" Grid.Column="4" GridLinesVisibility="None" FontWeight="SemiBold" FontSize="18"
>

<datagrid x:name="DgOutcome" xmlns:x="#unknown">
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="669" Width="150"
removed="{x:Null}"
BorderThickness="0"
HeadersVisibility="None" BorderBrush="{x:Null}" Margin="10,10,0,0" GridLinesVisibility="None" FontWeight="SemiBold" FontSize="18"
>

推荐答案

UI元素可以参考样式在XAML中使用 Style 属性,该值可以使用 StaticResource 。这是引用单独定义的样式的方式,并且对于相同类型的UI元素具有不同的样式。



请从这里开始:https://msdn.microsoft.com/en-us/library/ms745683%28v=vs.110% 29.aspx [ ^ ]。



此CodeProject手册也很有用: WPF导览 - 第5部分(样式) [ ^ ]。

首先,要注意对于上面引用的文章中的With Styles部分。你明白了吗?



-SA
UI elements can reference style in XAML using the Style attribute, which value can use StaticResource. This is the way to reference the style defined separately and have different styles for the same type of UI element.

Please start here: https://msdn.microsoft.com/en-us/library/ms745683%28v=vs.110%29.aspx[^].

This CodeProject manual can also be useful: A Guided Tour of WPF – Part 5 (Styles)[^].
First of all, pay attention for the section "With Styles" in the article referenced above. Are you getting the idea?

—SA


这篇关于在wpf中选择控件的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆