5

私のWPF ListView列でカラーコンボボックス(画像を参照)の動作を取得したい。 写真

誰かがこれを始めるのを手伝ってくれますか? ListView バインディングには慣れていますが、これを実装する方法がわかりません。

編集:

 xmlns:System="clr-namespace:System;assembly=mscorlib"

<ObjectDataProvider MethodName="GetValues"
                    ObjectType="{x:Type System:Enum}"
                    x:Key="ColorList">
   <ObjectDataProvider.MethodParameters>
       <x:Type TypeName="Windows.Media.Color" />
   </ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

提供されるタイプは列挙型でなければならないことを教えてくれます。

私が見つけたベストアンサー: XAML を使用して WPF で色を一覧表示するにはどうすればよいですか?

4

2 に答える 2

1

次のようになります。

 <ComboBox ItemsSource="{Binding ItemSourceObs}">     
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Rectangle Fill="{Binding Color}" Height="10"  Width="10" />
                        <TextBlock Text="{Binding DisplayedText}" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>    
        </ComboBox>

DisplayesText と Color (Brushes のタイプ) はオブジェクトのプロパティであり、ItemSourceObs はタイプ A の ObservableCollection であるとします。

このアプローチはMVVMパターンに基づいています

実用的なソリューションの背後にあるコードを使用する:

 <ComboBox x:Name="ComboColor" Width="50" Height="50"  >     
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
                    <TextBlock Text="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>    
    </ComboBox>

そして背後にあるコード:

public MainWindow()
    {
        InitializeComponent();
        ComboColor.ItemsSource = typeof(Colors).GetProperties();
    }
于 2015-03-25T18:50:06.973 に答える