此处将为大家介绍关于应用CSS和Html等WPF样式的详细内容,并且为您解答有关应用css样式的方式的相关问题,此外,我们还将为您介绍关于c#–WPF样式中的继承反转、css–TargetType上的
此处将为大家介绍关于应用CSS和Html等WPF样式的详细内容,并且为您解答有关应用css样式的方式的相关问题,此外,我们还将为您介绍关于c# – WPF样式中的继承反转、css – TargetType上的WPF样式选择器,指定嵌套目标类型、css和html 字符、css和html 字符_html/css_WEB-ITnose的有用信息。
本文目录一览:- 应用CSS和Html等WPF样式(应用css样式的方式)
- c# – WPF样式中的继承反转
- css – TargetType上的WPF样式选择器,指定嵌套目标类型
- css和html 字符
- css和html 字符_html/css_WEB-ITnose
应用CSS和Html等WPF样式(应用css样式的方式)
我正在编写一个简单的WPF应用程序,并希望我的样式在外部定义,因此您基本上可以将样式作为外部资源加载.有点像CSS和HTML通常分开…
然而,有一件事让我感到困惑,那就是HTML& CSS更像是一种外在的方法,CSS以元素为目标并应用于匹配元素.然而,对于WPF来说,它似乎是另一种方式,你必须告诉元素应该应用哪些样式,但这似乎不是很动态.所以我只想知道是否有一种方法可以应用样式,因此样式只是针对元素.
一个例子是:
<Style targettype="{x:Type TextBox}" TargetElement="{x:Name SomeTextBoxElement}"> <Setter Property="Width" Value="Auto"/> </Style>
通常你必须指定一个带有样式的键,然后获取要映射到样式键的元素(SomeTextBoxElement).
只是显然给每个元素一个样式绑定看起来有点傻了,它应该更加层次化并过滤掉(这可能是,我只是不知道如何)而不是一对一的映射.永远需要为整个应用程序设计风格……
希望有道理……
解决方法
<Style targettype="{x:Type TextBox}"> <Setter Property="Width" Value="Auto"/> </Style>
这会将您的样式应用于所有文本框,只需省略TargetElement属性.
我知道这不完全相同,但是如果你想要相同的基本控制风格,只需要根据用途进行一些更改,你可以尝试使用触发器对其进行微小的更改.我通常使用控件的“Tag”属性为我的样式定义提供一些额外的信息.示例基本按钮样式,但有3个按钮:
<Style.Triggers> <Trigger Property="Tag" Value="delete"> <Setter Property="Background" Value="Red"></Setter> </Trigger> <Trigger Property="Tag" Value="confirm"> <Setter Property="Background" Value="Green"></Setter> </Trigger> </Style.Triggers>
现在有些假设.如果满足两个条件,您想要的将非常容易实现:
>所有WPF视觉效果都有一个名为“ParentVisual”的DependencyProperty,返回视觉效果
立即包含有问题的视觉效果.
>所有WPF视觉效果都有DepencyProperty,比如说“VisualType”
鉴于此并将其与触发器/多重触发器组合,然后可以为ParentVisual.VisualType创建触发器.
让我们为WPF定义一些假设的CSS,将样式应用于包含在网格中包含Id“subnavi”的Border中包含的任何TextBox.
Grid Border#subnavi TextBox { backgroundColor:#FF0000; }
转换为WPF,这将是TextBox的Multitrigger,有三个条件:
<MultiTrigger> <Condition Property="ParentVisual.VisualType" Value="Border"></Condition> <Condition Property="ParentVisual.Name" Value="subnavi"></Condition> <Condition Property="ParentVisual.ParentVisual.VisualType" Value="Grid"></Condition> <Setter Property="Background" Value="Red"/> </MultiTrigger>
但不幸的是,没有这样的DependencyProperties,所以如果你为每个wpf控件创建包装器,你将只能使用我上面提到的机制.
回到我所谓的WPF思维模式:WPF元素由WPF引擎设置样式,从可视树开始,而不是向下,从相关控件开始.我认为CSS反过来了.
编辑:我认为在我上面写的内容中使用DependencyObject类比使用Visual更可取.
c# – WPF样式中的继承反转
<Button Content="Button"/>
和风格:
<Style targettype="Button"> <Setter Property="Background" Value="Blue"/> </Style>
父窗口(或另一个UserControl)可以设置另一种更通用的样式:
<Style targettype="Button"> <Setter Property="Background" Value="Red"/> </Style>
结果是(很明显)父按钮将具有更一般的样式(红色),而我的用户控件将具有更具体样式(蓝色)的按钮.
我想知道如何反转这种行为,以实现像我的自定义用户控件中设置默认样式,然后可以在父控件或窗口中覆盖,如果有必要?
关键是,默认样式首先在自定义用户控件中定义,并由其父级自动覆盖.这就是我称之为反转的方式.
解决方案的可能示例如下所示:
<Style targettype="Button" StylePriority="Default"> <Setter Property="Background" Value="Blue"/> </Style>
StylePriority可以指示如果没有为该按钮定义其他样式,则应该应用默认样式.
解决方法
一个UserControl:
<UserControl x:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example"> <UserControl.Resources> <Style targettype="local:UserControl1"> <Style.Resources> <Style targettype="Button" x:Key="UserControl1.DefaultButtonStyle"> <Setter Property="Background" Value="Red"/> </Style> </Style.Resources> </Style> </UserControl.Resources> <Button Content="UserControlButton"/> </UserControl>
还有一个窗口:
<Window x:xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Example"> <Window.Resources> <Style targettype="Button"> <Setter Property="Background" Value="Blue" /> </Style> </Window.Resources> <StackPanel> <local:UserControl1 > <local:UserControl1.Resources> <Style x:Key="UserControl1.DefaultButtonStyle" targettype="Button" BasedOn="{StaticResource {x:Type Button}}"> <Setter Property="FontSize" Value="40" /> </Style> </local:UserControl1.Resources> </local:UserControl1> <Button Content="WindowButton" /> </StackPanel> </Window>
如果在窗口中删除控件的样式,则将应用默认的用户控件按钮样式.
css – TargetType上的WPF样式选择器,指定嵌套目标类型
<Style targettype="Table"> <Style targettype="Paragraph"> <Setter Property="Margin" Value="0" /> <Style> </Style>
但是不允许嵌套样式.
我希望在CSS中实现的等价物如下:
Table Paragraph {margin:0}
因此,表格范围内的所有段落标记的边距都为0.这在WPF中是否可行(在XAML标记部分中)?复杂的WPF样式选择器的任何好的来源也将不胜感激.
我可以编写代码来完成它,但这不是我想要的.
解决方法
<Style targettype="Table"> <Style.Resources> <Style targettype="Paragraph"> <Setter Property="Padding" Value="0" /> </Style> </Style.Resources> </Style>
此样式将应用于范围内的所有表,嵌套的段落样式将应用于这些表内的所有段落,但不应用于表外的段落.这是因为将内容放在Resources属性中基本上将它们注入到所有子对象的可见范围内.
你也可以在技术上做这样的多层嵌套来为只在Paragraphs里面的Hyperlinks green上色,这只是在Tables里面:
<Style targettype="Table"> <Style.Resources> <Style targettype="Paragraph"> <Setter Property="Padding" Value="0" /> <Style.Resources> <Style targettype="Hyperlink"> <Setter Property="Foreground" Value="Green" /> </Style> </Style.Resources> </Style> </Style.Resources> </Style>
css和html 字符
网址http://www.mikaelnorling.se/character-cards/
兼容性网址: caniuse
css和html 字符_html/css_WEB-ITnose
网址http://www.mikaelnorling.se/character-cards/
兼容性网址: caniuse
我们今天的关于应用CSS和Html等WPF样式和应用css样式的方式的分享就到这里,谢谢您的阅读,如果想了解更多关于c# – WPF样式中的继承反转、css – TargetType上的WPF样式选择器,指定嵌套目标类型、css和html 字符、css和html 字符_html/css_WEB-ITnose的相关信息,可以在本站进行搜索。
本文标签: