[英]Why I can change a resource properties when it defined at Windows.Resources but not from Application.Resources
[英]Application.Resources in WPF from Template Copy - How can I get order into it?
我對 WPF 很陌生。 我需要更改鼠標懸停在 colors 上的按鈕,所以我右鍵單擊該按鈕,然后復制模板。 因此,Visual Studio 在我的App.xaml
中添加了很多新行,現在我可以在其中設置按鈕的設計,如下所示:
<Application x:Class="Thermologger.Start.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Thermologger.Start"
Startup="Application_Startup">
<!--StartupUri="MainWindow.xaml"-->
<Application.Resources>
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" StrokeDashArray="1 2" SnapsToDevicePixels="true" StrokeThickness="1" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#ff609d9f"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="#ff609d9f"/>
<SolidColorBrush x:Key="Button.Static.Foreground" Color="white"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="#cf609d9f"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#cf609d9f"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#cf609d9f"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="black"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
還有更多!
所以現在我想用另一個按鈕和其他 colors 來做到這一點 - 所以我再次做了同樣的事情,即當 Visual Studio 開始對其進行編號時:
<SolidColorBrush x:Key="Button.Static.Background" Color="#ff609d9f"/>
<SolidColorBrush x:Key="Button.Static.Background1" Color="#ff609d9f"/>
因為它是很多自動生成的線條,所以我的App.xaml
大約有 500 多條線長,只需在按鈕中進行一些顏色更改。
所以我的問題是,我現在怎樣才能做得更好?
這將有助於我能夠將每個按鈕提取到它自己的文件中以獲得更清晰的文件,例如SaveButton.xaml
或CancelButton.xaml
並在需要時重新導入它們。 那可能嗎?
創建一個新的資源字典Project > Add new item... > Resource Dictionary (WPF)並將其命名為SaveButton.xaml
。 將資源復制到這個新的資源字典中。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" StrokeDashArray="1 2" SnapsToDevicePixels="true" StrokeThickness="1" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<SolidColorBrush x:Key="Button.Static.Background" Color="#ff609d9f"/>
<!-- ...other resources. -->
</ResourceDictionary>
為另一個按鈕 styles 創建一個字典,並將它們添加到應用程序資源中。
<Application x:Class="Thermologger.Start.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Thermologger.Start"
Startup="Application_Startup">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="SaveButton.xaml"/>
<!-- ...other resource dictionaries. -->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
如果您只想更改 colors,則不需要復制按鈕 styles 和模板。 一種方法可能是為您的不同 styles 分離畫筆,如下所示:
ButtonStyles.xaml
。x:Key
重命名為BaseButtonStyle
。StaticResource
和x:Static
引用替換為DynamicResource
標記擴展(在運行時解析資源 - 這里是畫筆)BaseButtonStyle
的樣式,並將所有畫筆從基本樣式移動到新樣式的Resources
。x:Key
並調整畫筆。 像上面一樣將資源字典添加到應用程序資源中。 現在,您可以使用其x:Key
顯式引用代碼中的每個特殊按鈕樣式。
<StackPanel>
<Button Style="{DynamicResource SaveButtonStyle}" Content="Test"/>
<Button Style="{DynamicResource CancelButtonStyle}" Content="Test"/>
</StackPanel>
在運行時,畫筆將在邏輯樹上進行解析。 由於畫筆在基本樣式中不可用,因此在查找過程中可以在派生樣式中找到它們。 有關資源查找的更多信息,請參閱XAML 資源概述 (WPF .NET) 。
查找在由設置屬性的元素定義的資源字典中檢查請求的鍵 [...] 查找向上遍歷邏輯樹到父元素及其資源字典。 這個過程一直持續到到達根元素。 [...]
這是一個用於保存和取消按鈕的資源字典示例,帶有惰性着色。
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="FocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="2" StrokeDashArray="1 2" SnapsToDevicePixels="true" StrokeThickness="1" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="BaseButtonStyle" TargetType="{x:Type Button}">
<Setter Property="FocusVisualStyle" Value="{DynamicResource FocusVisual}"/>
<Setter Property="Background" Value="{DynamicResource Button.Static.Background}"/>
<Setter Property="BorderBrush" Value="{DynamicResource Button.Static.Border}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" SnapsToDevicePixels="true">
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" TargetName="border" Value="{DynamicResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource Button.MouseOver.Border}"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{DynamicResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{DynamicResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{DynamicResource Button.Disabled.Foreground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="SaveButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}">
<Style.Resources>
<SolidColorBrush x:Key="Button.Static.Background" Color="Red"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="Blue"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="Yellow"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="Purple"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="Green"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="Orange"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="Brown"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="Aqua"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="AntiqueWhite"/>
</Style.Resources>
</Style>
<Style x:Key="CancelButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource BaseButtonStyle}">
<Style.Resources>
<SolidColorBrush x:Key="Button.Static.Background" Color="Black"/>
<SolidColorBrush x:Key="Button.Static.Border" Color="DarkSlateGray"/>
<SolidColorBrush x:Key="Button.MouseOver.Background" Color="DimGray"/>
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="DarkGray"/>
<SolidColorBrush x:Key="Button.Pressed.Background" Color="LightGray"/>
<SolidColorBrush x:Key="Button.Pressed.Border" Color="Gray"/>
<SolidColorBrush x:Key="Button.Disabled.Background" Color="GhostWhite"/>
<SolidColorBrush x:Key="Button.Disabled.Border" Color="DimGray"/>
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="LightSlateGray"/>
</Style.Resources>
</Style>
</ResourceDictionary>
當然,如果需要,您也可以將它們拆分為幾個不同的資源字典。
我認為您可以在自定義按鈕樣式中使用類似以下的內容,或者創建針對按鈕的一般樣式,然后在您想要的按鈕中使用它。
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Green" />
</Trigger>
</Style.Triggers>
或者,您可以使用 MouseEnter 和 MouseLeave 事件並手動更改代碼中的顏色,但這通常不是首選
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.