【发布时间】: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 的选定值中为某些内容着色。谢谢!
【问题讨论】: