TdW
TdW

Reputation: 15

How to set the color/background of an Ellipse with a color chosen via a ComboBox?

This is in code behind window

public WpfTestWindow()
{
    InitializeComponent();
    ComboboxCOLOR.ItemsSource = typeof(Colors).GetProperties();
}

This is the ComboBox

<ComboBox Name="ComboboxCOLOR" Height="25"  Width="180">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Rectangle Fill="{Binding Name}" Width="16" Height="16"/>
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And - whatever color is chosen in ComboBox: It should fill up this Ellipse with that color; Here below what I got at the moment, it doesnt work...

<Ellipse Fill="{Binding ComboboxCOLOR}" Name="Ellipse"
         Margin="5" Height="40" Width="40" 
         Stroke="Black" StrokeThickness="5">
</Ellipse>

Upvotes: 0

Views: 121

Answers (1)

thatguy
thatguy

Reputation: 22079

You can refer to the ComboBox using the ElementName in the binding.

Gets or sets the name of the element to use as the binding source object.

If you do not specify the element name, the binding markup extension will interpret ComboboxCOLOR as a property on the current data context of the Ellipse element. Then use the SelectedItem property and the Name property of the item as in your example to bind the Fill

<Ellipse Fill="{Binding SelectedItem.Name, ElementName=ComboboxCOLOR}" Name="Ellipse"
         Margin="5" Height="40" Width="40" 
         Stroke="Black" StrokeThickness="5">
</Ellipse>

Upvotes: 2

Related Questions