风格没被拿起WPF

时间:2012-06-18 09:59:05

标签: c# wpf

我正在尝试在外部DLL中设置样式,用于定义某些控件的外观。

我有一个在外部DLL中定义的资源字典,其格式为TextBoxes:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type TextBox}" x:Key="TextStyle">
        <Setter Property="Text" Value="Moo"/>
    </Style>
</ResourceDictionary>
然后我在另一个应用程序中引用这个构建的DLL。这有效:

<Window x:Class="HTMLTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/GX3Resources;component/Resources.xaml"/>

            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>

    <Grid>
        <TextBox Height="23" HorizontalAlignment="Left" Margin="45,217,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" Style="{StaticResource TextStyle}"/>
    </Grid>
</Window>

这不是:

<Window x:Class="HTMLTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="pack://application:,,,/GX3Resources;component/Resources.xaml"/>

                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>

        <Grid>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="45,217,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        </Grid>
    </Window>

我希望上面会选择TextStyle,因为它是一个文本框,并且样式是以文本框为目标。

1 个答案:

答案 0 :(得分:2)

如果您可以编辑原始样式,可以通过将其键属性设置为目标类型来自动将其用于所有文本框:

<Style TargetType="{x:Type TextBox}" x:Key="{x:Type TextBox}">

如果您无法更改样式,请尝试基于它创建另一个样式:

<Style TargetType="{x:Type TextBox}" 
       BasedOn="{StaticResource TextStyle}" 
       x:Key="{x:Type TextBox}">
</Style>
相关问题