Get selected item in ComboBox

OK, so I have a ComboBox populated with a ComboBoxItem that contains Rectangular Content that 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 the brush from the Rectangle Fill (or at least the 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 through cmd.Fill, but I get a null exception.

So how can I get it? I need it to color something from the selected ComboBox value. Thank!

+4
source share
1 answer

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 .

+2

Source: https://habr.com/ru/post/1670463/


All Articles