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

  • Home
  • SEARCH
  • 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 1095857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:11:31+00:00 2026-05-17T00:11:31+00:00

I’ve got a custom ItemsControl in my project and I’m trying to write a

  • 0

I’ve got a custom ItemsControl in my project and I’m trying to write a style for it that combines a static list of items with a list of items on a Dependency Property on the control itself.

Here is the respective XAML in my Resource Dictionary:

<x:Array Type="{x:Type System:Object}" x:Key="Static_CloudItems">
    <Button>One</Button>
    <Button>Two</Button>
    <Button>Three</Button>
</x:Array>

<Style TargetType="{x:Type ControlsBase:CloudControl}" x:Key="BasicCloudStyle">
    <Setter Property="ItemsSource">
        <Setter.Value>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ControlsBase:CloudControl}}, Path=CloudItems}" />
                <CollectionContainer Collection="{StaticResource Static_CloudItems}" />
            </CompositeCollection>
        </Setter.Value>
    </Setter>
</Style>

And then the respective code in my controls/windows/whatever:

<ControlsBase:CloudControl Style="{DynamicResource BasicCloudStyle}">
    <ControlsBase:CloudControl.CloudItems>
        <x:Array Type="{x:Type System:Object}">
            <Button>Four</Button>
            <Button>Five</Button>
        </x:Array>
    </ControlsBase:CloudControl.CloudItems>
</ControlsBase:CloudControl>

The Idea is that the style should combine the static elements with whatever elements are defined on the per-instance edition of the control.

My issue is, The binding above does not work (and I’ve realized why too!) So I need a way to be able to bind to the style’s parent, but since the setter isn’t in the visual/logical tree, just a property I’m a bit puzzled about how to proceed.

  • 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-05-17T00:11:32+00:00Added an answer on May 17, 2026 at 12:11 am

    How about implementing an IValueConverter that builds the correct CompositeCollection for the Control behind the scenes? See sample below:

    XAML:

    <Window x:Class="StackOverflow.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:System="clr-namespace:System;assembly=mscorlib"
            xmlns:local="clr-namespace:StackOverflow"
            Title="MainWindow" Height="350" Width="525"
            x:Name="window">
        <Window.Resources>
            <x:Array Type="{x:Type System:Object}" x:Key="Static_CloudItems">
                <Button>One</Button>
                <Button>Two</Button>
                <Button>Three</Button>
            </x:Array>
    
            <local:MyItemsSourceConverter x:Key="myConverter"/>
            <Style TargetType="{x:Type local:MyControl}" x:Key="BasicCloudStyle">
                <Setter Property="ItemsSource" 
                        Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource myConverter}}"/>
            </Style>
        </Window.Resources>
    
        <local:MyControl Style="{StaticResource BasicCloudStyle}" >
            <local:MyControl.InstanceItems>
                <System:String>Item1</System:String>
                <System:String>Item2</System:String>
                <System:String>Item3</System:String>
            </local:MyControl.InstanceItems>
        </local:MyControl>
    </Window>
    

    Code-Behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
    
    public class MyControl : ListBox 
    {
        public ObservableCollection<object> InstanceItems
        {
            get { return (ObservableCollection<object>)GetValue(InstanceItemsProperty); }
            set { SetValue(InstanceItemsProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for InstanceItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InstanceItemsProperty =
            DependencyProperty.Register("InstanceItems", typeof(ObservableCollection<object>), 
            typeof(MyControl), new UIPropertyMetadata(new ObservableCollection<object>()));
    }
    
    public class MyItemsSourceConverter : IValueConverter
    {
        #region IValueConverter Members
    
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            MyControl mc = value as MyControl;
            if (mc != null)
            {
                CompositeCollection cc = new CompositeCollection();
    
                CollectionContainer container1 = new CollectionContainer();
                BindingOperations.SetBinding(container1, CollectionContainer.CollectionProperty,
                    new Binding()
                    {
                        Source = mc,
                        Path = new PropertyPath("InstanceItems")
                    });
                cc.Add(container1);
    
                CollectionContainer container2 = new CollectionContainer()
                {
                    Collection = mc.FindResource("Static_CloudItems") as Array
                };
    
                cc.Add(container2);
    
                return cc;
            }
            return null;
        }
    
        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new System.NotImplementedException();
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
i got an object with contents of html markup in it, for example: string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.