【问题标题】:Get Selected Item in ComboBox获取 ComboBox 中的选定项
【发布时间】:2017-02-21 20:42:28
【问题描述】:

好的,所以我有一个 ComboBox 填充了 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,但我得到一个 null 异常。

那么我怎样才能得到它呢?我需要它从 ComboBox 的选定值中为某些内容着色。谢谢!

【问题讨论】:

    标签: c# combobox uwp


    【解决方案1】:

    我不确定你为什么在组合框的内容中得到 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;
    

    请注意,ComboBox 通常与 ItemsSourceItemTemplate 一起使用,在这种情况下,您的代码可能如下所示:

    <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 如果你看看我的组合框,你会发现我已经设置了属性SelectedIndex="0"
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多