I am not sure why you are getting null in comboboxitem content. However, if you do it like this:
<ComboBox x:Name="comboBox" SelectedIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="70" Width="100">
<Rectangle Fill="#8BC34A" Width="50" Height="50"/>
<Rectangle Fill="#7CB342" Width="50" Height="50"/>
<Rectangle Fill="#689F38" Width="50" Height="50"/>
<Rectangle Fill="#558B2F" Width="50" Height="50"/>
<Rectangle Fill="#33691E" Width="50" Height="50"/>
</ComboBox>
then you can simply select the selected element in the Rectangle:
var selectedRectangle = comboBox.Items[comboBox.SelectedIndex] as Rectangle;
var selectedRectangle = comboBox.SelectedItem as Rectangle;
, ComboBox ItemsSource ItemTemplate, :
<ComboBox x:Name="comboBox" SelectedIndex="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="70" Width="100">
<ComboBox.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding Converter={StaticResource StringToBrushConverter}}" Width="50" Height="50"/>
</DataTemplate>
</ComboBox.ItemTemplate>
<x:String>FF8BC34A</x:String>
<x:String>FF7CB342</x:String>
<x:String>FF689F38</x:String>
<x:String>FF558B2F</x:String>
</ComboBox>
:
public class StringToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
string color = (string)value;
byte[] bytes = new byte[4];
for (int i = 0; i < color.Length; i += 2)
bytes[i / 2] = byte.Parse(color.Substring(i, 2), NumberStyles.HexNumber);
return new SolidColorBrush(Color.FromArgb(bytes[0], bytes[1], bytes[2], bytes[3]));
}
public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotImplementedException(); }
}
. , , , ComboBox .