Question

I have a number of XAML Brush objects defined in my project. Some are relatively detailed. Right now they're all located in (EDIT) Brushes.xaml, defined as a ResourceDictionary in a file of its own, like so:

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <LinearGradientBrush x:Key="FiveColorGradient"  >
          ...with five gradient stops
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="TwentyFourColorGradient" >
            ...with 24 gradient stops
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="RedYellowGradient" >
            ...etc
        </LinearGradientBrush>
  </ResourceDictionary>

The question is, how do I get those items loaded into an ItemsList control, such as a ComboBox?

(EDIT) I hope to leverage binding syntax right in the XAML, which will be easier to maintain. But I haven't hit on the right syntax. This is what I tried so far:

<UserControl.Resources>
    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary x:Key="BrushesDictionary" Source="Brushes.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</UserControl.Resources>
<Grid>
    <ComboBox ItemsSource="{StaticResource BrushesDictionary}"/>
</Grid>

Of course, that's kicking back errors, because you can't assign a key to a ResourceDictionary, or something.

(I've tried looking up several ideas, but none are supported by any documentation that isn't thoroughly buried in content aggregation or years of dust...)

Was it helpful?

Solution 2

I don't think you can get it to work using ResourceDictionary. But, using a CompositeCollection inside a ResourceDictionary did work:

    <CompositeCollection x:Key="Brushes" >
        <LinearGradientBrush />
        <LinearGradientBrush />
        <LinearGradientBrush />
        <LinearGradientBrush />
        <LinearGradientBrush />
    </CompositeCollection>

Then, the XAML to access it looks like this:

    <ComboBox    ItemsSource="{StaticResource Brushes}" HorizontalContentAlignment="Stretch" SelectedIndex="0">
        <ComboBox.ItemTemplate>
            <DataTemplate >
                <Rectangle Height="20" HorizontalAlignment="Stretch"  Fill="{Binding}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

OTHER TIPS

You can use x:Static to create the binding:

<ComboBox.ItemsSource>
          <Binding Path="Resources.Keys" 
                   Source="{x:Static Application.Current}"/>
</ComboBox.ItemsSource>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top