gpt4 book ai didi

c# - 如何使用单独的 WpfControlLibrary 中的 ResourceDictionary 将样式设置为 Prism 6 模块中的 RadioButton?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:18 25 4
gpt4 key购买 nike

在我的 Prism 6 WPF 模块化应用程序中,我使用名为“CommonControlLibrary”的 WPF ControlLibrary 项目,该项目具有包含 ResourceDictionary 的“SwitchButtonStyle.xaml”文件。ResourceDictionary 用于在我的应用程序中设置单选按钮的样式。以下是我的应用程序结构:

enter image description here

下面我部分展示了 ResourceDictionary。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommonControlLibrary"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" >

<!--Style for view-switching radiobuttons-->
<Style x:Key="MainViewRadioButtonStyle" TargetType="RadioButton">
<Setter Property="Background" Value="{x:Null}"/>
<Setter Property="Foreground" Value="#FF483D8B"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="BorderBrush" Value="#FF6A5ACD" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="RadioButton">
<Grid>
. . . . . . . . . . . . . . . .

我的应用程序“授权”和“校准”中有两个 Prism 6 模块(见上图)。每个模块都有对应的View和RadioButton来切换到那个View。下面我显示 RadioButton 切换到“校准” View 的 XAML:

<UserControl x:Class="Calibration.Views.CalibrationNavigationItemView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<RadioButton GroupName="MainNavigation" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}"
AutomationProperties.AutomationId="CalibrationRadioButton">
Calibration
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<prism:InvokeCommandAction Command="{Binding NavigateToCalibrationCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</RadioButton>
</Grid>

XAML 中的两个 RadioButton 是相同的,并且每个 RadioButton 都需要使用 ResourceDistionary 中的 MainViewRadioButtonStyle 进行样式设置,该样式位于“CommonControlLibrary”中的“SwitchButtonStyle.xaml”中。如何在每个 RadioButton 的 XAML 中包含对 ResourceDictionary 的引用,以将“MainViewRadioButtonStyle”样式应用于 Prism 6 模块中的 RadioButton?请在“校准”RadioButton XAML 示例中向我展示它。

最佳答案

1) 在 View 顶部为合并的资源字典添加 xaml 代码:

<UserControl.Resources>

<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>

<ResourceDictionary Source="pack://application:,,,/CommonControlLibray;component/SwitchButtonStyle.xaml" />

2.) 为单选按钮添加样式属性:

<RadioButton Style={StaticResource MainViewRadioButtonStyle}" ...

为单选按钮添加隐式样式:

<Style TargetType="RadioButton" BasedOn="{StaticResource MainViewRadioButtonStyle}" />

隐式样式将应用于 View 上的所有 RadioButton

关于c# - 如何使用单独的 WpfControlLibrary 中的 ResourceDictionary 将样式设置为 Prism 6 模块中的 RadioButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com