2010-11-24 8 views
0

私はWP7のためのXAMLを持っている:どのようにバインドさリストボックス内の項目にアクセスするには、WP7

<ListBox x:Name="lbMain" DataContext="{Binding}" ItemsSource="{Binding Items}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock x:Name="txtName" Text="{Binding name}" /> 
        <ListBox x:Name="lbCars" DataContext="{Binding}" ItemsSource="{Binding cars}" ScrollViewer.VerticalScrollBarVisibility="Disabled"> 
         <ListBox.ItemTemplate> 
          <DataTemplate> 
           <StackPanel x:Name="spnlCars"> 
            <TextBlock x:Name="txtCarName" Text="{Binding name}" /> 
            <ListBox x:Name="lbCarColor" DataContext="{Binding}" ItemsSource="{Binding color}"> 
             <ListBox.ItemTemplate> 
              <DataTemplate> 
               <TextBlock x:Name="txtColor" Text="{Binding colorValue}"/> 
               <Image Name="imgColor"/> 
              </DataTemplate> 
             </ListBox.ItemTemplate> 
            </ListBox> 
           </StackPanel> 
          </DataTemplate> 
         </ListBox.ItemTemplate> 
        </ListBox> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

マイDataContexは、Webサービスからデータを取得している、私が作成したのViewModelに設定されています。データ構造は次のとおりです。

  • マシン(あり:車は、[])
  • -Vehicles(あり:名前、車[]、トラックは[]、...)----私は何を結合していますthatsの----例えば、色[0] = "赤"
  • --- colorValue

I:lbMain

  • --Cars(名前、色[]、...有する)にimgColorに入れたい画像リソースもあります。

    私は知りませんホットへ:

    1. txtColorに応じてリソースとは異なる画像を取得するために、各imgColorを設定し、
    2. がtxtCarNameに太字フォントを適用すると、「txtColor.Text =(たとえば)赤"。

    私はアドバイスと提案を感謝します。

  • 答えて

    2

    Converterを作成し、色名をBitmapImageに変換します。

    例:

    class ColorToImageConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
         String colorName = (String)value; 
    
         switch (colorName.ToLower()) 
         { 
          case "red": 
           return new BitmapImage(new Uri("...")); 
          default: 
           return new BitmapImage(new Uri("...")); 
         } 
        } 
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
         throw new NotImplementedException(); 
        } 
    } 
    
    +0

    素晴らしいdecycloneだ、ありがとう!それが私の最初の質問を探していた答えです。 私の2番目の質問に対する助けがありますか?親DataContexにアクセスしようとしたときに問題が発生し、古いListBoxItemを変更しようとしました。 – Slavisa

    +0

    同じ方法でコンバーターを使用する必要があります。つまり、FontWeightをカラーにバインドし、コンバーターでColorをFontWeightにバインドします。 –

    +0

    私はFrancescoに同意します。上記の例では 'BitmapImage'型の代わりに' FontWeight'型に色名を変換するために別のコンバータが必要です。 – decyclone

    関連する問題