好的,我有一个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 值来为某些内容着色。谢谢!
SelectedIndex="0"
。 - Romasz