Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7773585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:09:57+00:00 2026-06-01T17:09:57+00:00

I have several hundred brushes defined in Application.xaml. These are shared resources for several

  • 0

I have several hundred brushes defined in Application.xaml. These are shared resources for several user controls in project. They all have patterned keys: ch_YSD, ch_HJU, ch_IYO…

I try to use these brushes in datatemplate. In datatemplate I have access to variable part of brushes keys, that is in data template I can get YSD, HJU, IYO, etc. as string.

How can I bind to particular brush resource from xaml?

Currently I have such solution: in style use data trigger to set required property to specified brush depending on bound string (variable part of keys).
I am not satisfied with this solution because list of brushes in Application.xaml will increase often.

I am not sure if I should go for code behind as I want to use memory saving benefit of shared resources in Application.xaml.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T17:09:58+00:00Added an answer on June 1, 2026 at 5:09 pm

    It’s a kind of task that looks simple, but actually has no perfect solution (or I didn’t find it). The first approach is to use value converter. But it doesn’t work! We need to set resource reference which can’t be done correctly by converter. So, I think the right way is attached behavior. But you should know about limitation: there can be only one property to apply resource to. Perhaps you could avoid this limitation depending on your requirements.

    Attached behavior allows you to reference resource with a specified named to a specified dependency property:

    public static class BrushResourceKeyBehavior
    {
        #region ResourceKey Property
    
        public static readonly DependencyProperty ResourceKeyProperty = DependencyProperty.RegisterAttached(
            "ResourceKey", typeof(object), typeof(BrushResourceKeyBehavior),
            new FrameworkPropertyMetadata(OnResourceKeyChanged));
    
        public static object GetResourceKey(DependencyObject dependencyObject)
        {
            return dependencyObject.GetValue(ResourceKeyProperty);
        }
    
        public static void SetSource(DependencyObject dependencyObject, object value)
        {
            dependencyObject.SetValue(ResourceKeyProperty, value);
        }
    
        #endregion
    
        #region TargetProperty Property
    
        public static readonly DependencyProperty TargetPropertyProperty = DependencyProperty.RegisterAttached(
            "TargetProperty", typeof(DependencyProperty), typeof(BrushResourceKeyBehavior),
            new FrameworkPropertyMetadata(OnTargetPropertyChanged));
    
        public static DependencyProperty GetTargetProperty(DependencyObject dependencyObject)
        {
            return (DependencyProperty)dependencyObject.GetValue(TargetPropertyProperty);
        }
    
        public static void SetTargetProperty(DependencyObject dependencyObject, DependencyProperty value)
        {
            dependencyObject.SetValue(TargetPropertyProperty, value);
        }
    
        #endregion
    
        private static void OnResourceKeyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var targetProperty = GetTargetProperty(dependencyObject);
            if (targetProperty != null)
            {
                if (e.NewValue == null)
                {
                    dependencyObject.ClearValue(targetProperty);
                }
                else
                {
                    SetResourceReference(dependencyObject, targetProperty);
                }
            }
        }
    
        private static void OnTargetPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var oldValue = e.OldValue as DependencyProperty;
            var newValue = e.NewValue as DependencyProperty;
    
            if (oldValue != null)
            {
                dependencyObject.ClearValue(oldValue);
            }
    
            if (newValue != null)
            {
                SetResourceReference(dependencyObject, newValue);
            }
        }
    
        private static void SetResourceReference(DependencyObject dependencyObject, DependencyProperty targetProperty)
        {
            var fe = dependencyObject as FrameworkElement;
            if (fe != null)
            {
                fe.SetResourceReference(targetProperty, String.Format("ch_{0}", GetResourceKey(fe)));
            }
            else
            {
                var fce = dependencyObject as FrameworkContentElement;
                if (fce != null)
                {
                    fce.SetResourceReference(targetProperty, String.Format("ch_{0}", GetResourceKey(fce)));
                }
            }
        }
    }
    

    Behavior can be used in XAML like the following:

    <ItemsControl>
        <Border local:BrushResourceKeyBehavior.Source="YSD"
                local:BrushResourceKeyBehavior.TargetProperty="Border.Background"
                Height="20"/>
        <Border local:BrushResourceKeyBehavior.Source="HJU"
                local:BrushResourceKeyBehavior.TargetProperty="Border.Background"
                Height="20"/>
        <Border local:BrushResourceKeyBehavior.Source="IYO"
                local:BrushResourceKeyBehavior.TargetProperty="Border.Background"
                Height="20"/>
    </ItemsControl>
    

    The code above is equivalent to:

    <ItemsControl>
        <Border Background="{DynamicResource ch_YSD}"
                Height="20"/>
        <Border Background="{DynamicResource ch_HJU}"
                Height="20"/>
        <Border Background="{DynamicResource ch_IYO}"
                Height="20"/>
    </ItemsControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is used on several hundred computers across the company
I have several hundred documents with this information: => User(id: integer, email: string, amount:
I have a CSS file which contains several hundred 16x16 icons. They are referenced
I have a PDF document that has several hundred fields. All of the field
I have several hundred files in a folder. Each of these file is a
I have several hundred files in a non-flat directory structure. My Makefile lists each
I have a list on SharePoint with several hundred items in it. I also
I have an company asp.net website with textboxes which may contain several hundred words.
I have several thread pools and I want my application to handle a cancel
I have several hundred images that I ended up saving as a large file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.