gpt4 book ai didi

wpf - 如何使 WPF 资源样式在运行时可切换?

转载 作者:行者123 更新时间:2023-12-04 02:08:58 26 4
gpt4 key购买 nike

我在 WPF 中使用了样式资源,因此在我的 App.xaml 中我可以定义是使用客户布局还是管理员布局。通过以类似 HTML/CSS 的方式从主 XAML 中获取所有样式,这可以很好地工作。

现在我如何使这个动态以便我可以单击应用程序中的按钮并在运行时在客户和管理员之间切换布局?

附录:

感谢 Meeh 提供的链接,所以我做了这个,当我调试时,它会遍历所有行,但 仍然没有改变布局 ,我还需要做什么才能在运行时更改布局?

private void Button_Switch_Layout_Click(object sender, RoutedEventArgs e)
{
string layoutIdCode = "administrator";

switch (layoutIdCode)
{
case "administrator":
langDictPath = "Resources/AdministratorLayout.xaml";
break;

case "customer":
langDictPath = "Resources/CustomerLayout.xaml";
break;
}

Uri langDictUri = new Uri(langDictPath, UriKind.Relative);
ResourceDictionary langDict = Application.LoadComponent(langDictUri) as ResourceDictionary;
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(langDict);
}

原始代码:

应用程序.xaml:
<Application x:Class="TestMvvm8837.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/CustomerLayout.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

Window1.xaml:
<Window x:Class="TestMvvm8837.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Style="{StaticResource MainWindow}"
Title="Resource Test" >
<DockPanel>

<Grid Style="{StaticResource Form}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="0" Text="First Name" />
<TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="1" Text="Last Name" />
<TextBlock Style="{StaticResource FormLabel}" Grid.Column="0" Grid.Row="2" Text="E-Mail" />

<TextBox Grid.Column="1" Grid.Row="0"/>
<TextBox Grid.Column="1" Grid.Row="1"/>
<TextBox Grid.Column="1" Grid.Row="2"/>

<Button Style="{StaticResource FormSaveButton}" Grid.Column="1" Grid.Row="3" Content="Save"/>

</Grid>

</DockPanel>
</Window>

管理员布局.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="MainWindow" TargetType="{x:Type Window}">
<Setter Property="Width" Value="800"/>
<Setter Property="Height" Value="600"/>
</Style>

<Style x:Key="Form" TargetType="{x:Type Grid}">
<Setter Property="Margin" Value="20"/>
</Style>

<Style x:Key="FormLabel" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="14"/>
</Style>

<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="200"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>

<Style x:Key="FormSaveButton" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="14"/>
<Setter Property="Width" Value="100"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>

</ResourceDictionary>

CustomerLayout.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<Style x:Key="MainWindow" TargetType="{x:Type Window}">
<Setter Property="Width" Value="600"/>
<Setter Property="Height" Value="480"/>
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="1.156,1.133" StartPoint="-0.033,0.019">
<GradientStop Color="#FFFFFFFF" Offset="0"/>
<GradientStop Color="#FF4FADD3" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>

<Style x:Key="Form" TargetType="{x:Type Grid}">
<Setter Property="Margin" Value="5"/>
</Style>

<Style x:Key="FormLabel" TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="10"/>
</Style>

<Style TargetType="{x:Type TextBox}">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="200"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>

<Style x:Key="FormSaveButton" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="10"/>
<Setter Property="FontFamily" Value="Verdana"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Width" Value="100"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="Cursor" Value="Hand"/>
</Style>

</ResourceDictionary>

最佳答案

您需要使用 DynamicResource 在 Window1.xaml 中,而不是 StaticResource . StaticResource只计算一次,而 DynamicResource实际上监听变化。

关于wpf - 如何使 WPF 资源样式在运行时可切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/733605/

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