2016-10-16 6 views
0

Silverlight ComboBoxに画像とテキストを表示したいとします。 WPFでItemTemplateを使ってイメージと名前で色を表示した例が見つかりました。 Silverlightでは、同じxmlが空白行になります。したがって、アイテムごとにアイテムが生成され、Nameプロパティにバインドされません。 SilverlightにはWPF以外のバインディングが必要ですか? Color動作しませんの名前に結合することによってRectangleFillを設定しようとsilverlightコンボボックスのアイテムテンプレートバインディング

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     cmbColors.ItemsSource = typeof(Colors).GetProperties(); 
    } 
} 

XML

<UserControl x:Class="SilverlightColors.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400"> 

    <Grid x:Name="LayoutRoot" Background="White"> 
     <StackPanel > 
      <ComboBox Name="cmbColors" > 
       <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> 
     </StackPanel> 
    </Grid> 
</UserControl> 

答えて

0

これはサンプルです。 XAMLはいくつかの特別な魔法を使って、以下を取得します:

<Rectangle Fill="White" Width="16" Height="16" Margin="0,2,5,2"/> 

GetProperties()から返されたPropertyInfoの "Name"プロパティは "Black"、 "white"または "Yellow"ですが、直接使用することはできません。あなたがしなければならないことは、名前とブラシの辞書を作成し、それぞれに異なる色を割り当ててから、コンボボックスのデータソースをそれにバインドすることです。

このコードは動作します:

は.cs:

var list = typeof(Colors).GetProperties(); 
var brushes = new Dictionary<string, SolidColorBrush>(); 
foreach (var colour in list) 
{ 
    brushes.Add(colour.Name, new SolidColorBrush((Color)colour.GetValue(colour, null))); 
} 
cmbColors.ItemsSource = brushes; 

XAML:

<ComboBox Name="cmbColors" 
      VerticalAlignment="Center" 
      HorizontalAlignment="Center"> 
    <ComboBox.ItemTemplate > 
     <DataTemplate > 
      <StackPanel Orientation="Horizontal"> 
       <Rectangle Fill="{Binding Value}" Width="16" Height="16" Margin="0,2,5,2"/> 
       <TextBlock Text="{Binding Key}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 
関連する問題