在ComboBox中获取所选项目

4

好的,我有一个ComboBox,里面装着一些ComboBoxItem,这些ComboBoxItem包含了一个矩形内容,其中还有一个填充字段:

<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>

我需要从矩形填充中获取画刷(或至少是填充的字符串值)。所以我尝试了这个:

var comboBoxItem = comboBox.Items[comboBox.SelectedIndex] as ComboBoxItem;
var cmb = comboBoxItem.Content as Windows.UI.Xaml.Shapes.Rectangle;

我尝试使用 cmd.Fill 获取 Fill,但是出现了空指针异常。

那么我该如何获取它呢?我需要通过选定的 ComboBox 值来为某些内容着色。谢谢!

1个回答

2

我不确定为什么comboboxitem的内容会出现null。然而,如果您按照以下方式操作,应该可以解决:

<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>

那么你只需要将选定的项目简单地转换为矩形

var selectedRectangle = comboBox.Items[comboBox.SelectedIndex] as Rectangle;
var selectedRectangle = comboBox.SelectedItem as Rectangle;

请注意,通常ComboBoxItemsSourceItemTemplate一起使用,此时您的代码可能如下所示:
<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绑定到颜色字符串集合。


哇,非常感谢!第一件事就帮了我大忙!我不知道你可以这样填充ComboBox。再次感谢! - stroibot
还有一个问题,我如何使一个矩形默认为选中状态? - stroibot
@stroibot 如果您查看我的组合框,那么您会发现我已经设置了属性 SelectedIndex="0" - Romasz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接