我想在我的应用程序中使用自定义主题,据我所知,我可以通过使用资源字典并在App.xaml中引用它来实现此目的.样式将覆盖默认值,如下所示:
<Style TargetType="{x:Type Label">
<Setter Property="Foreground" Value="Green" />
</Style>
Run Code Online (Sandbox Code Playgroud)
现在我猜我的默认标签样式是用相同的值覆盖但我的所有标签字体都是绿色.当我想再次在某个地方设置一个标签时,问题就出现了.当我想像我这样更改网格中的其他属性时
<Grid.Resources>
<Style TargetType="{x:Type Label">
<Setter Property="FontSize" Value="28" />
</Style>
</Grid.Resources>
Run Code Online (Sandbox Code Playgroud)
我的网格中的所有标签都会丢失其前景色并再次出现默认值(我是否在上一步中覆盖了默认值?).经过一些尝试,我发现要正确地做到这一点,我必须添加另一个属性来Style声明BasedOn={StaticResource {x:Type Label}}",它的工作原理.这对我来说有点奇怪,因为现在我将不得不在整个应用程序中重复相同的BasedOn代码,这不是样式的工作原理 - 这应该自动完成!例如,在HTML + CSS样式中继承和合并,在WPF中它们被替换...
请注意,当我不使用任何样式控件时,仍然可以从somehwere(System Themes?)中获取它们的外观.我怎么能告诉他们在其他地方寻找默认值,所以如果没有任何额外的样式代码,他们会认为默认情况下它们应该是绿色的?
有什么方法可以自动设置BasedOn属性吗?或者也许有更好的做到这一点?
如何在代码中设置以下内容?
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
Run Code Online (Sandbox Code Playgroud)
我正在使用App.xaml中合并的主题.它适用于所有控件,但是当我为某些东西定义样式时,例如TextBox,除非我BasedOn像上面一样使用主题样式,否则它将获得默认TextBox样式.
现在我正在创建一个DataGridTextColumn代码背后,我无法让它的BasedOn部分工作EditingElementStyle
Style editingStyle = new Style(typeof(TextBox));
editingStyle.BasedOn = ...?;
Run Code Online (Sandbox Code Playgroud)
有什么建议?此外,有没有办法获得主题样式而不是使用默认样式而不使用BasedOn?
谢谢
可能重复:
Datatemplate继承
我有几个不是子类的数据类型,它们也不共享一个接口,但它们确实有我希望在XAML DataTemplate中显示的公共属性.那就是说,我知道这是可能的......
<!-- Basic Style Inheritance -->
<Style x:Key="FooStyle" TargetType="Foo" />
<Style x:Key="EnhancedFooStyle" TargetType="Foo" BasedOn="{StaticResource FooStyle}" />
<!-- Inheritance By Type -->
<Style x:Key="BaseItemStyle">
<Setter Property="Control.Background" Value="Yellow" />
</Style>
<!-- These three data types share the same 'BaseItemStyle' -->
<Style TargetType="ListBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="ComboBoxItem" BasedOn="{StaticResource BaseItemStyle}" />
<Style TargetType="TreeViewItem" BasedOn="{StaticResource BaseItemStyle}" />
Run Code Online (Sandbox Code Playgroud)
但是对于没有BasedOn属性的数据模板,我们可以做类似的事吗?
<DataTemplate x:Key="CommonTemplate">
<!-- Common Stuff Goes Here -->
</DataTemplate>
<!-- These three datatypes share the same DataTemplate -->
<DataTemplate DataType="Foo1" BasedOn="{StaticResource …Run Code Online (Sandbox Code Playgroud) 对于我来说,为数据输入表单的样式编写类似的东西并不罕见,但我的问题是,TextBox并且TextBlock似乎没有实现在中的Setter BaseElementStyle.通常我需要单独定义它们.
为什么是这样?它有办法吗?
我猜它与其他控件模板中常用的事实有关(例如,TextBlock用于大多数控件,TextBox用于DatePickers和ComboBoxes)
<Style x:Key="BaseElementStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Margin" Value="5" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseElementStyle}" />
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseElementStyle}" />
Run Code Online (Sandbox Code Playgroud) 我们正在编写TextBox的自定义子类,我们只需要更改该样式的一些基本部分.现在我们知道你何时进行子类化Control,通常你应该像这样更改元数据......
public class EnhancedTextBox : TextBox
{
static EnhancedTextBox()
{
// Commenting this line out lets this use the default TextBox style.
DefaultStyleKeyProperty.OverrideMetadata(
typeof(EnhancedTextBox),
new FrameworkPropertyMetadata(typeof(EnhancedTextBox)));
}
}
Run Code Online (Sandbox Code Playgroud)
这会将用于该子类类型的键更改为类型本身,这意味着它不再获取默认的TextBox样式.没什么大不了的...我们只是评论该行,它再次起作用,因为键仍然设置为TextBox直接使用的值.
但是,我们想知道我们是否确实想要改变风格中的一些东西,而不是整个风格,我们可以简单地创建一个新的风格并设置它的BasedOn属性......但是我们在那里设置了什么?我们不想手动拔出XAML并创建一个新的样式,给它一个键,然后使用StaticResource,但我们还没有找到我们可以在那里设置说'这是基于TextBox样式.
我希望它简单,但我们还没有找到它.有人可以帮忙吗?
我正在创建一个基本风格.我正在使用继承来实现我的基本样式.我知道如何在派生类上添加属性,但我不知道如何更改属性.让我给你举个例子:
假设我有这种基本风格:
<!-- SrollViewer ScrollBar Repeat Buttons (at each end) -->
<Style x:Key="ScrollBarLineButton" TargetType="{x:Type RepeatButton}">
<Setter Property="SnapsToDevicePixels" Value="True"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RepeatButton}">
<Border
Name="Border"
Margin="1"
CornerRadius="2"
Background="WhiteSmoke"
BorderThickness="1">
<Image Stretch="Uniform" Source="/FilesPro;component/Images/scrollArrow.png" Height="40" VerticalAlignment="Center" HorizontalAlignment="Center" Width="52" >
<Image.RenderTransform>
<TransformGroup>
<ScaleTransform ScaleY="-1" />
<TranslateTransform Y="40"/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform/>
</TransformGroup>
</Image.RenderTransform>
</Image>
<!--
<Path
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="{StaticResource GlyphBrush}"
Data="{Binding Path=Content,
RelativeSource={RelativeSource TemplatedParent}}" >
</Path>
-->
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
我想创建一个新的样式,它具有与ScrollBarLineButton相同的属性,但是图像transfrom scaleY = 1而不是-1.
当我做: …
我正在开发一个 WPF 应用程序,并创建了一个自定义控件,我们将其称为“CControl”。在我设计应用程序布局的 xaml 文档中。我使用以下方式导入样式:
xmlns:my="clr-namespace:My.Controls"
Run Code Online (Sandbox Code Playgroud)
并且我能够很好地使用该控件。问题是我想扩展 CControl 上的样式。在资源词典中,我可以设置:
<Style TargetType="{x:Type my:CControl}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
Run Code Online (Sandbox Code Playgroud)
这会将样式应用于 Control,但不会导入 CControl 定义的样式,因此我使用:
<Style TargetType="{x:Type my:CControl}" BasedOn="{StaticResource {x:Type my:CControl}}">
<Setter Property="Margin" Value="5 0 5 3" />
</Style>
Run Code Online (Sandbox Code Playgroud)
问题是当我的框架尝试加载 xaml 时,出现以下异常:
System.Windows.Markup.XamlParseException occurred
Message='Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '18' and line position '54'.
Source=PresentationFramework
LineNumber=18
LinePosition=54
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri …Run Code Online (Sandbox Code Playgroud) 比如说,我有一个TextBox“TextBoxStyleBase”的默认样式。然后,我定义一个DataGrid样式,该样式具有TextBox基于该基本样式的自己的样式,并定义另一个边框颜色。
在 a 内部的某个地方,DataGrid我想定义另一种TextBox样式,但继承于 style 中定义的样式DataGrid。
有没有办法使样式继承当前为当前“上下文”中的特定控件定义的样式?
编辑:
为了更清楚地说明,这是我所拥有的:
<!-- explicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
<Setter Property="FontSize" Value="16"/>
</Style>
<!-- implicit style for all TextBoxes -->
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}"/>
<!-- DataGrid style changing inner TextBox style -->
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextStyle}">
<Setter Property="FontSize" Value="20"/>
</Style>
<!-- since TextBox has defined implicit style this would be equivalent …Run Code Online (Sandbox Code Playgroud) basedon ×8
wpf ×7
styles ×5
inheritance ×4
xaml ×4
c# ×1
coding-style ×1
datatemplate ×1
skin ×1
subclass ×1
themes ×1