C# xaml resourcedictionary内容设置程序前台未正确显示
我试图制作一些标准化颜色方案等的ResourceDictionary,以添加到dll类库中,供将来的应用程序使用。我是XAML新手,在创建或使用字典的内容设置器部分时似乎犯了一个错误。我无法设置和使用字典中文本的颜色。这是我到目前为止所拥有的;如您所见,内容中文本块的前景设置为白色C# xaml resourcedictionary内容设置程序前台未正确显示,c#,xaml,resourcedictionary,staticresource,C#,Xaml,Resourcedictionary,Staticresource,我试图制作一些标准化颜色方案等的ResourceDictionary,以添加到dll类库中,供将来的应用程序使用。我是XAML新手,在创建或使用字典的内容设置器部分时似乎犯了一个错误。我无法设置和使用字典中文本的颜色。这是我到目前为止所拥有的;如您所见,内容中文本块的前景设置为白色 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:My_Class_Library.WPF_Resources">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../WPF Resources/Buttons.xaml"/>
<ResourceDictionary Source="../WPF Resources/Brushes.xaml"/>
<ResourceDictionary Source="../WPF Resources/Sliders.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="myButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button" >
<Grid>
<Rectangle Name="ClickFill" Fill="ForestGreen" RadiusX="5" RadiusY="5"/>
<ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding Content}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Content">
<Setter.Value>
<Grid>
<TextBlock Background="{x:Null}" Foreground="White"></TextBlock>
</Grid>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
以下是对字典的参考:
<Grid x:Class="My_Class_Library.Update_Information"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:My_Class_Library"
HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../WPF Resources/Buttons.xaml"/>
<ResourceDictionary Source="../WPF Resources/Brushes.xaml"/>
<ResourceDictionary Source="../WPF Resources/Sliders.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Grid.Resources>
<Button Name="Click"
Width="100" Height="30"
Style="{StaticResource myButton}"
Click="Click_Click">
_click
</Button>
</Grid>
_点击
然而,我看到的是:
,如您所见,它具有黑色(假定为默认)文本,而不是指定的白色文本。我做错了什么,导致内容无法设置
我知道每个人都讨厌像“看看这个,它怎么了?”这样的问题,但我想找到一个解决办法,但我已经束手无策了——我正在跟踪大量的培训视频等等,以上是我最大的努力。。。几乎所有我尝试的东西都破坏了这一切!非常感谢任何指点 您的想法是正确的,但是您需要习惯直接将样式模板中的属性设置为setter。我还建议您从控件的默认样式模板开始,并根据您的需要对其进行编辑,因为例如,在这种情况下,您将失去鼠标悬停等所有额外的视觉状态 但是,为了解决您的直接问题,您将把
内容
属性全部从模板中删除(这是您的ContentPresenter
工作),而只需执行此操作(伪操作)
…瞧。希望这有帮助,干杯 的确如此;工作完美。特别感谢你更一般性的评论——这类东西在现阶段对我来说是黄金;我已经准备好养成坏习惯了!当你说“默认样式模板”时,你是指内置的模板,还是建议我制作一个,并使用
BasedOn
进一步构建?不用担心,我们都有过。在学习过程中,我建议从默认方法开始,直到你掌握了模板结构。例如,在本例中,我右键单击一个控件->编辑模板,然后选择是要一个特定于实例的本地副本还是在资源字典中。最终,当您在所有地方应用模板时,您会希望将它们放到资源字典中,并在需要时使用“基于”扩展您创建的默认值。通常需要一些修补和混合是超级方便,当它得到更多的参与。祝你好运!
<Style x:Key="myButton" TargetType="Button">
<Setter Property="Foreground" Value="White"/>
<!-- Rest of it goes here -->
</Style>