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

时间:2009-04-09 10:10:09

标签: wpf xaml styles

我在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);
}

原始代码:


App.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>

AdministratorLayout.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>

4 个答案:

答案 0 :(得分:14)

您需要在Window1.xaml中使用DynamicResource而不是StaticResourceStaticResource仅评估一次,而DynamicResource实际上会监听更改。

答案 1 :(得分:0)

你应该:

  • 使用加载资源字典 您已定义的XamlReader 新风格
  • 从加载的资源中获取对新样式的引用(newStyle =(Style)resource [“styleName])
  • 在控件上设置新样式:control.Style = newStyle

HTH

再见

答案 2 :(得分:0)

您可以使用ContentControl - 此处它会根据ViewType变量更改xaml。

<ContentControl Style="{StaticResource ScriptsViewTemplate}" VerticalAlignment="Stretch" />   


<Style x:Key="ScriptsViewTemplate" TargetType="ContentControl">
<Style.Triggers>
  <DataTrigger Binding="{Binding Path = ViewType,Mode=TwoWay}" Value="True">
    <Setter Property="Template" Value="{StaticResource ScriptsTreeViewTemplate}"/>
  </DataTrigger>
  <DataTrigger Binding="{Binding Path = ViewType,Mode=TwoWay}" Value="False">
    <Setter Property="Template" Value="{StaticResource ScriptsListViewTemplate}"/>
  </DataTrigger>
</Style.Triggers>
</Style>

答案 3 :(得分:-3)

Use resource dictionaries

..和switch them at runtime:)

祝你好运

(哦,对不起我的懒惰:P)