Can't access resource from Generic.xaml within the Custom Control constructor.

Posted by myermian on Stack Overflow See other posts from Stack Overflow or by myermian
Published on 2010-05-26T21:00:12Z Indexed on 2010/05/26 21:01 UTC
Read the original article Hit count: 276

I'm not sure why this is doing this, but I can't access the resource from within my constructor.

XTabItem.cs

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace MyStuff
{
    public class XTabItem : TabItem
    {
        public static readonly DependencyProperty XTabItemNormalBackgroundProperty;

        /// <summary>
        /// Visual Property: Normal Background
        /// </summary>
        [Description("Determines the visibility of the close button."), Category("XTabItem Visual")]
        public Brush XTabItemNormalBackground
        {
            get { return (Brush)GetValue(XTabItemNormalBackgroundProperty); }
            set { SetValue(XTabItemNormalBackgroundProperty, value); }
        }

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

            XTabItemNormalBackgroundProperty = DependencyProperty.Register("XTabItemNormalBackground", typeof(Brush), typeof(XTabItem),
                new UIPropertyMetadata(null));

        }

        public XTabItem()
        {
            //XTabItemNormalBackground = (Brush)this.TryFindResource("XTabItemNormalBackgroundBrush"); //THIS DOES NOT WORK??
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
        }
    }
}

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyStuff"
    xmlns:con="clr-namespace:MyStuff.Converters"
    >

    <SolidColorBrush x:Key="XTabItemNormalBackgroundBrush" Color="BlueViolet" />

    <Style TargetType="{x:Type local:XTabItem}">
       <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:XTabItem}">

                    <!-- CONTENT TEMPLATE -->
                    <Grid SnapsToDevicePixels="True">
                        <Border x:Name="_Border" Background="{Binding Path=XTabItemNormalBackground, RelativeSource={RelativeSource Mode=TemplatedParent}}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="1,1,1,0">

                            ...

                        </Border>
                    </Grid>

    </Style>

</ResourceDictionary>

© Stack Overflow or respective owner

Related posts about wpf

Related posts about xaml