2017-02-21 73 views
4

OK,所以我必須充滿ComboBoxItem組合框包含其中有一個Fill場矩形內容:獲取所選項目的ComboBox

<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得到填充,但我得到一個空例外。

那我該如何得到它?我需要它從選擇的ComboBox值中着色。謝謝!

回答

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; 

平時注意組合框作品與的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(); } 
} 

在這種情況下,您選擇的項目將是一個。對於你的場景,第一種方法可能更容易,但對於更復雜的場景,這會更好,請注意,在這種情況下,您可以輕鬆地將組合框綁定到帶有顏色的字符串集合。

+0

哇,非常感謝!第一件事幫助了我!我不知道你可以像這樣填充ComboBox。再次感謝! – stroibot

+0

還有一個問題,我怎樣才能使其中一個矩形默認選中? – stroibot

+0

@stroibot如果你看看我的組合框,那麼你會看到我已經設置了屬性'SelectedIndex =「0」'。 – Romasz