设想:
我想用3个标准字体大小为我的WPF应用程序:BigFontSize,NormalFontSize,和SmallFontSize。这些是双值,它们在资源字典中定义为(sys适当定义的地方):
<sys:Double x:Key="BigFontSize">18</sys:Double>
<sys:Double x:Key="NormalFontSize">14</sys:Double>
<sys:Double x:Key="SmallFontSize">12</sys:Double>
Run Code Online (Sandbox Code Playgroud)
这很好用。但是我随机选择了 14 个作为正常尺寸。我想要的是获得系统定义的字体大小NormalFontSize。(如果这样做了,我可以使用此处描述的转换器来获取BigFontSize和SmallFontSize相对于NormalFontSize)
线索:
我从文档中发现默认字体大小存储在静态属性 (double) 中SystemFonts.MessageFontSize。但是我应该怎么做才能将该值检索到资源字典中?(我知道Binding或DynamicResource不能适用。但是,嘿,这是一个静态值,所以我怎么能申请StaticResource或x:Static或什么?)
我试过了
<sys:Double x:Key="XXXFontSize">
<StaticResource ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
Run Code Online (Sandbox Code Playgroud)
和
<sys:Double x:Key="XXXFontSize">
<x:Static ResourceKey="SystemFonts.MessageFontSize" />
</sys:Double>
Run Code Online (Sandbox Code Playgroud)
两者似乎都不起作用(如预期)。我收到一条错误消息Cannot add content to object of type 'System.Double'.
笔记:
我不想将其封装在可以从中派生出其他样式的通用样式中(使用BasedOn),因为我有多个资源字典,并且无法使用DynamicResourcewithBasedOn
也就是说,我不能使用 …
我有一个 ResourceDictionary,例如 (MyResourceDictionary):
<ResourceDictionary xmlns
.....
>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SeriesTwo.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="SeriesDetailedInformation">
<StackPanel>
......content...
</StackPanel>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
SeriesTwo.xaml 看起来像这样,并且还有同名的 DataTemplate
<ResourceDictionary xmlns=
.....
>
<DataTemplate x:Key="SeriesDetailedInformation">
<DataGrid>
......content...
</DataGrid>
</DataTemplate>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在我的视图页面上,SeriesDetailedInformation使用哪个数据模板?这是否取决于我在页面上首先引用的资源字典?
任何有关该主题的良好链接和其他阅读材料也将受到赞赏。
我将 ReSharper 8.2.3 完整版与 Visual Studio Ultimate 2013 和 .NET 4.5 一起使用。
背景是我有一个包含大量项目的C# WPF解决方案。为了简化这里的描述,我有以下项目:
项目 1:包含 UI 资源(样式、字体等)的库,包括名为 ResourceLibrary.xaml 的资源字典。
项目 2:带有一些 WPF 用户控件/视图和 app.xaml 的主 exe。WPF 用户控件不直接引用项目 1 中的 ResourceLibrary.xaml。IntelliSense 工作正常,ReSharper 不会生成任何警告(蓝色波浪线)。
项目 3:带有一些 WPF 用户控件/视图的库。所有这些用户控件都参考 ResourceLibrary.xaml:
<UserControl.Resources>
<ResourceDictionary Source="/<my_resource_namespace>;component/ResourceLibrary.xaml" />
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
IntelliSense 工作正常,ReSharper 不会生成任何警告(蓝色波浪线)。
该解决方案构建良好,IntelliSense 工作(在 ResourceLibrary.xaml 中查找静态资源)并且 ReSharper 不会生成任何警告。
但是,如果删除以上几行,IntelliSense 将不起作用,ReSharper 会生成警告“找不到资源 XXX”。请注意,项目 2 中的 XAML 文件也使用资源库中的资源,但情况并非如此。该解决方案构建良好。
由于性能原因,我想避免在每个 XAML 文件中包含资源库,但我确实希望 ReSharper 给我相关警告。这个问题有解决方法吗?
我知道我可以在 ReSharper 中关闭“找不到资源 XXX”警告,但这会阻止我检测到真正的资源问题。
wpf resources resourcedictionary visual-studio resharper-8.0
在我的wpf应用程序中,我想为所有TextBox设置样式。结果,我的App.xaml看起来像:
<Application x:Class="IM.WindowsApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Foreground" Value="Yellow"/>
</Style>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
在MainWindow.xaml中,我具有:
<TextBox Text="Some Text" />
Run Code Online (Sandbox Code Playgroud)
当我运行该程序时,它的文本显示为黄色,与我预期的一样。
现在这是我的问题
现在,我想向该文本框添加一些其他样式。结果,我将代码修改为如下所示
<TextBox Text="Some Text">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Opacity" Value=".8" />
</Style>
</TextBox.Style>
</TextBox>
Run Code Online (Sandbox Code Playgroud)
当我运行时,我的文本框前景不再是黄色:(。我不想替换样式。
一种解决方案是为原始样式提供资源密钥。然后我可以放置BasedOn={StaticRecource MyResourceKey}。这不是解决方案,因为我必须Style="{StaticResource MyResourceKey}"在我的应用程序中添加到所有文本框。我想避免这样做。
URI 前缀无法识别
我在System.NotSupportedException.
此行用于设置 a 的来源ResourceDictionary:
view.Source = new Uri("/DA.EasyTeach.Math;component/View.xaml", UriKind.RelativeOrAbsolute);
Run Code Online (Sandbox Code Playgroud)
View.xaml具有生成操作的资源。
ResourceDictionary我想使用带有 的样式来定义控件的一般外观和感觉DataTrigger。
是否可以DataTrigger在视图中指定绑定路径?如果没有,是否有一些巧妙的替代方案可以实现我的目标?
我的目标是重用图形定义(包括触发器),但每次使用触发器时将触发器链接到不同的数据源。
样式示例:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Rectangle" x:Key="LedBehavior">
<Setter Property="Fill" Value="LightGray"/>
<Style.Triggers>
<DataTrigger Binding="{Binding **DefineThisPathInTheView**}" Value="True">
<Setter Property="Fill" Value="DarkGreen"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
在视图中我希望使用这种风格有点像这样:
<Rectangle Width="50" Height="50"
Style="{StaticResource LedBehavior}"
DataTriggerBindingPath="**PropertyInViewModel**"/>
Run Code Online (Sandbox Code Playgroud)
谢谢你!
我有以下ResourceDictionary(缩短):
<ResourceDictionary>
<Fluent:RibbonContextualTabGroup Header="Dokument" x:Key="dokumentRibbonGroup" x:Name="dokumentRibbonGroup" />
<Fluent:RibbonTabItem x:Key="dokumentRibbonTab" Header="Start" />
<Fluent:RibbonTabItem x:Key="ueberpruefenRibbonTab" Header="Überprüfen" />
<Fluent:RibbonTabItem x:Key="austauschRibbonTab" Header="Austausch" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
当我访问Keys字典的属性时,键按以下顺序:
dokumentRibbonTab austauschRibbonTab ueberpruefenRibbonTab dokumentRibbonGroup
我无法理解那个顺序.它不是按字母顺序排列的,不是修订版,尤其不是原始订单.有人有提示吗?或者甚至更好,关于如何指定项目顺序的建议ResourceDictionary?
我正在尝试从我的资源字典后面的代码设置ResourceDictionary DataContext.
我有一个使用自己的样式(资源字典)的数据模板,该样式包含一个具有自己样式的复选框:
<Style x:Key="CheckBoxStyle" TargetType="CheckBox">
<EventSetter Event="CheckBox.Checked" Handler="CheckBox_Checked"/>
<EventSetter Event="CheckBox.Unchecked" Handler="CheckBox_Unchecked"/>
</Style>
Run Code Online (Sandbox Code Playgroud)
在CheckBox_Checked事件中,我想引用字典的父(用户控件)视图模型来执行函数,但是因为资源字典没有DataContext属性从控件事件中设置DataContext,如下所示:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MyViewModel viewModel = (MyViewModel)DataContext;
}
Run Code Online (Sandbox Code Playgroud)
不起作用(当然).
我想我需要获得Ancestor(资源字典用户控件)的句柄,但不知道如何做到这一点 - 或者可能有另一种方式..
谢谢
乔
我正在阅读有关ResourceDictionary的内容,并且已经陷入了困惑.
看来我每个XAML只能有1个ResourceDictionary.所以,如果我想使用多个,我可以合并ResourceDictionaries.
如果我可以合并词典那么"全球"风格应该在哪里生活?我可以拥有一个ApplicationResourceDictionary,其中包含在我的应用程序中保持一致的所有样式,或者我可以将此信息保存到App.xaml文件中.两者似乎都是有效的选项,但我不知道是否是这种情况.
这是关于个人选择还是比另一个更好?看起来将它们保存在ResourceDictionaries中会更好,因为所有样式都在一起(在字典中)而不是在XAML页面中拆分一些.
在我的词典文件中
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="Foreground" Value="White" />
<Setter Property="Background" Value="Black" />
</Style>
Run Code Online (Sandbox Code Playgroud)
在我的Window xaml文件中,我有
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/XXX;component/XXX.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="Label" BasedOn="{StaticResource {x:Type Label}}">
<Setter Property="FontSize" Value="20"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
</ResourceDictionary>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
在设计师中我可以看到标签有黑色背景和白色前景,但在运行时它有默认的黑色前景和透明背景.很明显,我想继承字典样式,但它不起作用.我在那里做错了什么?我正在使用VS 2013和.NET 4.5
编辑:如果我删除Windows.Resources中的样式,那么将应用字典样式.如果我将样式从Windows资源移动到包含一些标签的StackPanel.Resource,那么继承可以正常工作