使用代码背后的另一个程序集中的样式

时间:2015-02-03 13:38:35

标签: c# wpf

我有一个cs-project-file,里面有ResourceDictionaryResourceDictionary的标记看起来像:

<ResourceDictionary x:Class="BaseLib.Services.TabControlServices.TabStyles"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/BaseLib.WPF.Skinning;component/BaseStyles.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

在此ResourceDictionary我正在加载位于另一个程序集中的另一个ResourceDictionary。我想要使​​用的部分如下:

<Style TargetType="{x:Type TabItem}" x:Key="tabItemStyle">
    <Setter Property="BorderThickness" Value="3" />
    <Setter Property="BorderBrush" Value="Green" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid>
                    <Border x:Name="border" CornerRadius="15,0,0,0"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1,1,1,0" />
                    <Border BorderThickness="{TemplateBinding BorderThickness}"
                            Padding="{TemplateBinding Padding}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <ContentPresenter ContentSource="Header" Grid.Column="0"
                                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                            <Button Grid.Column="1" Width="16" Height="16" Margin="5,0,0,0"
                                    Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"
                                    VerticalContentAlignment="Top" VerticalAlignment="Top"
                                    HorizontalContentAlignment="Left" HorizontalAlignment="Left"
                                    Padding="-4,-4,0,0" Command="{Binding CloseTabCommand}"
                                    CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=TabItem}}">
                                <Image Source="{x:Static imageProvider:Images.Close}" Width="14" Height="14" />
                            </Button>
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter TargetName="border" Property="Background" Value="{StaticResource borderColor}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我在cs代码中创建一个新的TabItem,如:

TabItem tabItem = new TabItem
{
   Header = DateTime.Now.ToString("dd.MM.yyyy");
};
tabControl.Items.Add(tabItem);
tabItem.IsSelected = true;

现在我想将tabItemStyle分配给代码生成 - TabItem

我试过了:

tabItem.Style = Application.Current.FindResource("tabItemStyle") as Style;

但不幸的是,这不起作用。我得到Exception:无法找到资源“tabItemStyle”。

我现在的问题是: 如何在另一个程序集中的cs-code中使用Style?

3 个答案:

答案 0 :(得分:0)

由于您将具有资源的字典合并到同一控件的xaml中。尝试:

this.FindResource("tabItemStyle") as Style;

答案 1 :(得分:0)

我在this post

的帮助下自己解决了这个问题

我现在分配Style的代码如下:

ResourceDictionary resourceDictionary = new ResourceDictionary
{
    Source = new Uri("/BaseLib.Services.TabControlServices;component/TabStyles.xaml", UriKind.RelativeOrAbsolute)
};
tabItem.Style = resourceDictionary["TabItemStyle"] as Style;

答案 2 :(得分:0)

确保按照以下步骤操作。 1.在尝试使用该样式的项目中添加对程序集的引用 2.在App.xaml中添加主资源字典 如下

 <Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/BaseLib.WPF.Skinning;component/BaseStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>      
</Application.Resources>

3。然后尝试在您的代码中添加样式,如上所述。