0

所以在我的主应用 ResourceDictionary 中,我的风格以

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

现在我正在做的是覆盖 TabControl 以制作自定义控件(没有任何 xaml)。问题是此时它没有继承自定义 TabControl 模板。

所以我想知道的是,我如何以编程方式绑定到模板的“x:Key”,考虑到它绑定到特定控件而我的控件没有 xaml 文件。

网上有些地方说要这样做

this.Style = (Style)FindResource("TabItem");

但这似乎不适用于我的情况。“样式”在一个单独的文件中,并导入到我的 App.Xaml 资源字典中......所以它正确地覆盖了所有 TabItems,但不是我覆盖的那个。

这是我的 App.xaml

<Application x:Class="Octgn.OctgnApp" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

  <Application.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>

        <ResourceDictionary Source="/Resources/Themes/Full/ExpressionDark.xaml"/>
       </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
  </Application.Resources>
</Application>

并且来自 ExpressionDark.xaml 的项目(略微截断,因为它很大)

<Style d:IsControlPart="True" TargetType="{x:Type TabItem}" x:Key="{x:Type TabItem}">
    <Setter Property="Foreground" Value="{DynamicResource TextBrush}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid x:Name="grid" Margin="2,1,2,3">
                    <Grid.LayoutTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                            <SkewTransform AngleX="0" AngleY="0"/>
                            <RotateTransform Angle="0"/>
                            <TranslateTransform X="0" Y="0"/>
                        </TransformGroup>
                    </Grid.LayoutTransform>
                    <Border x:Name="border" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0.5">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,0.976" StartPoint="0.5,0.039">
                                <GradientStop Color="#7F595959" Offset="0" />
                                <GradientStop Color="#19FFFFFF" Offset="1" />
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <Border x:Name="SelectedBorder" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0" Background="{DynamicResource SelectedBackgroundBrush}"/>
                    <Border x:Name="HoverBorder" BorderBrush="{x:Null}" CornerRadius="2,2,2,2" Opacity="0">
                        <Border.Background>
                            <LinearGradientBrush EndPoint="0.5,0.976" StartPoint="0.5,0.039">
                                <GradientStop Color="#7F595959" Offset="0" />
                                <GradientStop Color="#19FFFFFF" Offset="1" />
                            </LinearGradientBrush>
                        </Border.Background>
                    </Border>
                    <Grid>
                        <ContentPresenter x:Name="ContentSite" RecognizesAccessKey="True" ContentSource="Header" d:LayoutOverrides="Width, Height" HorizontalAlignment="Center" Margin="6,1,6,1" VerticalAlignment="Center" />
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

此样式自动应用于 TabItem,但我试图将其应用于我的 TabItem 覆盖。

这是代码

/// <summary>
/// The chat bar item.
/// </summary>
public class ChatBarItem : TabItem
{
    /// <summary>
    /// Sets the Chat Room
    /// </summary>
    private readonly NewChatRoom room;

    /// <summary>
    /// Initializes static members of the <see cref="ChatBarItem"/> class.
    /// </summary>
    static ChatBarItem()
    {
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(ChatBarItem), new FrameworkPropertyMetadata(typeof(TabItem)));
    }   

    /// <summary>
    /// Initializes a new instance of the <see cref="ChatBarItem"/> class.
    /// </summary>
    /// <param name="chatRoom">
    /// The chat Room.
    /// </param>
    public ChatBarItem(NewChatRoom chatRoom)
    {
        this.room = chatRoom;
        this.ConstructControl();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="ChatBarItem"/> class.
    /// </summary>
    public ChatBarItem()
    {
        this.room = null;
        this.ConstructControl();
    }

    /// <summary>
    /// Constructs this control
    /// </summary>
    private void ConstructControl()
    {
        //this.Background = Brushes.Transparent;
        //this.BorderThickness = new Thickness(0);
        //this.Style = (Style)FindResource("TabItem");
        // this is where I want to set the style of this control

        // Main content object
        var mainBorder = new Border { Margin = new Thickness(5) };

        // Main content grid
        var g = new Grid();
        g.ColumnDefinitions.Add(new ColumnDefinition());
        g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(16) });

        // Create item label
        var label = new TextBlock() { VerticalAlignment = VerticalAlignment.Center };
        if (this.IsInDesignMode() || this.room == null)
        {
            label.Inlines.Add(new Run("test"));
        }
        else
        {
            label.Inlines.Add(new Run(this.room.GroupUser.User.User));
        }

        // Create close button
        var borderClose = new Border { Width = 16, Height = 16 };
        var imageClose = new Image()
            {
                Source = new BitmapImage(new Uri("pack://application:,,,/Octgn;component/Resources/close.png")), 
                Stretch = Stretch.Uniform
            };

        // --Add items to items

        // Add close image to closeBorder
        borderClose.Child = imageClose;

        // Add Close 'button' to grid
        g.Children.Add(borderClose);
        Grid.SetColumn(borderClose, 1);

        // Add label to main grid
        g.Children.Add(label);

        // Add main grid to main border
        mainBorder.Child = g;

        // Add main grid to this
        this.Header = mainBorder;
    }
}

如果我有 TabItem 的 xaml,我可以轻松地使用 Style="{DynamicResource {x:Type TabItem}}"(或类似的东西),但我是以编程方式完成的。

4

3 回答 3

1

您已经尝试过:

<Style TargetType="{x:Type TabControl}" BasedOn="{StaticResource {x:Type TabControl}}">

</Style>

顺便说一句,您不需要定义 x:Key,如果您希望将样式应用于所有 TabControl,并且如果您希望它适用于您的整个应用程序,请在 App.xaml 中设置样式


编辑:我仍然不明白你想要做什么。(如果可以,请再次编辑您的问题)。

但是您可以尝试通过以下方式手动设置样式:

Style customStyle = (Style)Application.Current.FindResource("StyleKey");

如前所述,

static MyCustomTabControl()
{
     DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomTabControl), new FrameworkPropertyMetadata(typeof(TabControl)));
} 

但这仅在以下情况下才有效:

1)您的控件在文件中的样式:Themes\Generic.xaml

2) 文件“Generic.xaml”具有属性“BuildAction”,值 = Page。

3) AssemblyInfo.cs 包含:

[assembly: ThemeInfo (
     ResourceDictionaryLocation.None,
     ResourceDictionaryLocation.SourceAssembly
)]
于 2012-09-22T17:53:24.873 回答
1

在您的派生选项卡控件的静态构造函数中如何处理

static MyCustomTabControl()
{
     DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomTabControl), new FrameworkPropertyMetadata(typeof(TabControl)));

}    
于 2012-09-22T18:16:26.137 回答
0

好的,经过一番挖掘,以及这里其他两个答案的帮助,我能够弄清楚这一点。而不是为 FindResource 做一个字符串,看起来资源字典实际上是一个带有对象键的字典。所以...

this.Style = (Style)Application.Current.FindResource(typeof(TabItem));

这为我做到了。

再次感谢其他两个答案。

于 2012-09-22T20:56:16.620 回答