Reputation: 868
OK, so I have a ComboBox filled with ComboBoxItem which contain Rectangular Content which have a Fill field:
<ComboBox x:Name="comboBox" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="70" Width="100">
<ComboBoxItem IsSelected="True">
<ComboBoxItem.Content>
<Rectangle Fill="#8BC34A" Width="50" Height="50"/>
</ComboBoxItem.Content>
</ComboBoxItem>
<ComboBoxItem>
<Rectangle Fill="#7CB342" Width="50" Height="50"/>
</ComboBoxItem>
<ComboBoxItem>
<Rectangle Fill="#689F38" Width="50" Height="50"/>
</ComboBoxItem>
<ComboBoxItem>
<Rectangle Fill="#558B2F" Width="50" Height="50"/>
</ComboBoxItem>
<ComboBoxItem>
<Rectangle Fill="#33691E" Width="50" Height="50"/>
</ComboBoxItem>
</ComboBox>
And I need to get a Brush from a Rectangle's Fill (or at least a string value of Fill). So I tried this:
var comboBoxItem = comboBox.Items[comboBox.SelectedIndex] as ComboBoxItem;
var cmb = comboBoxItem.Content as Windows.UI.Xaml.Shapes.Rectangle;
And then get Fill via cmd.Fill, but i get a null exception.
So how can I get it? I need it to color something from a chosen value of ComboBox. Thank you!
Upvotes: 4
Views: 738
Reputation: 29792
I'm not sure why you get null in comboboxitem's content. However, should work 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 just simply cast selected item to Rectangle:
var selectedRectangle = comboBox.Items[comboBox.SelectedIndex] as Rectangle;
var selectedRectangle = comboBox.SelectedItem as Rectangle;
Note that usually ComboBox works with ItemsSource and ItemTemplate, in that case your code can look like this:
<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>
and the converter:
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(); }
}
In that case your selected item will be a string. For your scenario the first method may be easier, but for more complicated scenarios this will be much better, note also that in that case you can easily bind ComboBox to collection of string with colors.
Upvotes: 2