运行时的WPF动态用户控件样式

时间:2014-05-21 14:10:38

标签: wpf

我有一个带有togglebutton和几个文本块/标签的用户控件。我的xaml中有两种样式用于切换按钮样式。我加载表单时动态添加这些用户控件。我希望能够在我的应用程序中在运行时应用不同的样式。我无法弄清楚如何做到这一点。这是我的用户控件:

<UserControl x:Class="UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="122" Width="170" BorderThickness="1"> 
<UserControl.Resources>
    <Style x:Key="Test2" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" Background="Magenta"  BorderBrush="#FF000000" BorderThickness="1,1,1,1" SnapsToDevicePixels="True">
                        <ContentPresenter x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Background" TargetName="border" Value="Gainsboro"/>


                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="Test3" TargetType="{x:Type ToggleButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToggleButton}">
                    <Border HorizontalAlignment="Center" VerticalAlignment="Center" x:Name="border" Background="Beige"  BorderBrush="#FFF30808" BorderThickness="4,4,4,4" SnapsToDevicePixels="True">
                        <ContentPresenter x:Name="contentPresenter"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Background" TargetName="border" Value="Gainsboro"/>


                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
   </UserControl.Resources>

<Grid Margin="0,0,10,10">
    <WrapPanel HorizontalAlignment="Left" d:DesignHeight="133" VerticalAlignment="Top" d:DesignWidth="172" Margin="0,0,-11,-10">
        <ToggleButton x:Name="btnUserButton"  HorizontalAlignment="Left" VerticalAlignment="Top" d:DesignWidth="229" d:DesignHeight="101" ClipToBounds="true" Width="166" Height="100" Click="onButtonClick" Background="#FFA29E9E" Style="{StaticResource Test2}" >
            <StackPanel>
                <TextBlock Text="Title" d:DesignFontSize="35" d:DesignHeight="40" d:DesignWidth="201" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap"  ClipToBounds="true" FontSize="30"/>
                <TextBlock Text="Body" d:DesignFontSize="35" d:DesignHeight="40" d:DesignWidth="201" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" TextWrapping="Wrap" Name="cavityNumber"  ClipToBounds="true" FontSize="30"/>
            </StackPanel>
        </ToggleButton>
        <Label x:Name="lblYield" Content="Label"  VerticalAlignment="Top" d:DesignWidth="106" HorizontalContentAlignment="Center" Foreground="Black" d:DesignFontSize="20" d:DesignHeight="38"  ClipToBounds="true" Width="81" />
        <Label x:Name="lblScrap" Content="Label" HorizontalAlignment="Left" VerticalAlignment="Top" HorizontalContentAlignment="Center" Foreground="Red"  ClipToBounds="true" Width="76"/>
    </WrapPanel>
</Grid>

我已经为我的控件添加了“样式”属性,但我不知道是否需要它或者如何使用它。

    Property thestyle() As Style
    Get
        Return mystyle
    End Get
    Set(value As Style)
        mystyle = value
        btnUserButton.Style = mystyle
    End Set
End Property

我想要做的是在运行时以某种方式访问​​样式'Test2'和'Test3'。这些样式将基于用户选择。

MyUserControl.Style = DirectCast(FindResource("Test2"), Style)

当然,这不起作用。我在这里阅读有关应用程序资源的信息,但我不确定如何将其合并到我的usercontrol中。无论如何,谢谢你能给我的任何帮助......这让我很尴尬因为我对WPF有些新意。再次谢谢。

1 个答案:

答案 0 :(得分:1)

是的,最简单,最好的方法是使用资源。要在您的应用中使用资源,请先在项目中的某处创建一个xaml文件。称之为Styles这将是您可以转储所有样式的地方,然后您可以在不同的用户控件/网格等上重复使用它们。

<强> Styles.xaml

在xaml资源文件中,添加样式。它看起来应该类似于:

    <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation">

        <!-- LayoutRoot Grid Style (Example Style) -->
        <Style x:Key="LayoutRootGridStyle" TargetType="Grid">
          <Setter Property="Background" Value="#FFFFFFFF"/>
        </Style>

    </ResourceDictionary>

<强>的App.xaml

然后在App.xaml / Application.xaml中,您必须添加对此新资源字典的引用。

例如在App.xaml中:

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

现在,您可以使用在资源字典中创建的样式。

您可以在后面的代码中使用它们:

Dim st As Style = TryCast(Application.Current.Resources("MyButtonStyle"), Style)

myButton.Style = st

或直接在XAML中:

 <Button Style="{StaticResource MyStyleName}" />