2017-10-14 8 views
1

私は解決できない小さな問題があります。 (私はかなりWPFのnoobishであり、それはデータバインディングシステムです)私は現在、単純な列を持つDataGridですが、テンプレート列も挿入しなければならなかったので、アイコンとその名前も表示します。ラベルと画像をDataGridTemplateColumn内のコンボボックスにバインドするにはどうしたらいいですか?

すべてのデータバインディングは、ちょうど1つを除いて、正常に動作しています。

そこで質問です:

私はデータグリッド内のDataGridTemplateColumn内部のコンボボックスにイメージと自分の名前をバインドするにはどうすればよいです。

ご協力いただきありがとうございます。 :)

XAML:

<Window x:Class="Octopus.OctopusManagerWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:core="clr-namespace:System;assembly=mscorlib" 
      mc:Ignorable="d" Width="696" Background="#FF2B2B2B" Height="28" MinHeight="500"> 
    <Window.Resources> 
     <DataTemplate x:Key="iconTemplate" > 
      <WrapPanel Margin="0 0 0 0" Height="auto"> 
       <Image Width="18" Height="18" Stretch="Fill" Source="{Binding Image}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,15,0"/> 
       <Label Content="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"/> 
      </WrapPanel> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="auto" /> 
     </Grid.RowDefinitions> 
     <DataGrid x:Name="buttonDataGrid" ItemsSource="{Binding}" CanUserAddRows="False" CanUserSortColumns="False" 
        CanUserDeleteRows="False" CanUserReorderColumns="False" Margin="10" Grid.Row="1" SelectedIndex="1" 
        Width="auto" Height="auto" AutoGenerateColumns="False" Background="Transparent" Foreground="Gray"> 
      <DataGrid.Columns> 
       <DataGridTextColumn x:Name="id" Binding="{Binding id}" IsReadOnly="True" Header="ID" Width="26"/> 
       <DataGridCheckBoxColumn x:Name="isActive" Binding="{Binding isActive}" Header="Active" Width="42"/> 
       <DataGridTemplateColumn x:Name="leftIcon" Width="100" IsReadOnly="True" Header="Left Icon"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox> 
           <ComboBox.ItemTemplate> 
            <DataTemplate> 
             <ComboBox x:Name="leftIconList" 
                ItemTemplate="{Binding iconTemplate}" 
                ItemsSource="{Binding leftIconList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/> 
            </DataTemplate> 
           </ComboBox.ItemTemplate> 
          </ComboBox> 
         </DataTemplate>  
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTemplateColumn Width="100" IsReadOnly="True" Header="Right Icon"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <ComboBox> 
           <ComboBox.ItemTemplate> 
            <DataTemplate> 
             <ComboBox x:Name="rightIconList" 
                ItemTemplate="{Binding iconTemplate}" 
                ItemsSource="{Binding rightIconList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"/> 
            </DataTemplate> 
           </ComboBox.ItemTemplate> 
          </ComboBox> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 
       <DataGridTextColumn x:Name="btnText" Binding="{Binding btnText}" Header="Button Text" Width="*"/> 
       <DataGridTextColumn x:Name="Commands" Binding="{Binding command}" Header="Command To Execute" Width="*"/> 
      </DataGrid.Columns> 
     </DataGrid> 
    </Grid> 
</Window> 

のC#:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Data; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 

namespace myApp 
{ 
    public partial class myWindow : Window 
    { 
     ObservableCollection<buttonData> btnsData = new ObservableCollection<buttonData>(); 

     // buttonData class contains all necessary data to create a button 
     public class buttonData 
     { 
      // get set methods 
      public string id { get; set; } 
      public bool isActive { get; set; } 
      public List<icon> leftIconList { get; set; } 
      public List<icon> rightIconList { get; set; } 
      public string btnText { get; set; } 
      public string command { get; set; } 
     } 

     public class icon 
     { 
      public string Image { get; set; } 
      public string Name { get; set; } 
     } 

     private List<icon> _iconList; 
     public List<icon> iconList 
     { 
      get { return _iconList; } 
      set { _iconList = value; } 
     } 

     // init 
     // ------------------------------------------------------------------------------ 
     public myWindow() 
     { 
      InitializeComponent(); 

      Uri[] UriArray = new Uri[8]; 
      string[] fileNames = new string[8]; 

      // fill the iconList 
      for (int i = 0; i < fileListLength-1; i++) 
      { 
       string image = (string)UriArray.GetValue(i); 
       string name = (string)fileNames.GetValue(i); 
       iconList.Add(new icon { Image = image, Name = name }); 
      } 

      for (int i = 0; i < 8; i++) 
      { 
       buttonData btnDat = new buttonData(); 
       btnDat.id = (i+1).ToString(); 
       btnDat.isActive = true; 
       btnDat.leftIconList = iconList; 
       btnDat.rightIconList = iconList; 
       btnDat.btnText = ""; 
       btnDat.command = ""; 
       btnsData.Add(btnDat); 
      } 

      buttonDataGrid.DataContext = btnsData; 
     } 
    } 
} 

答えて

1

与えられたコードを持ついくつかの問題があります。これらの間違いを修正するために以下の手順を実行すると、あなたが望むものを得ることができます。

1. UriArrayは同じタイプのicon.Imageと定義する必要があります。 UriArrayfileNamesにいくつかのデータを入力することを忘れないでください。

string[] UriArray = new string[8]; 

2.アイコンリストを書き込む前にiconListを初期化します。

iconList = new List<icon>(); 

ComboBoxに非常に多くの階層を使用3.Don't、ちょうどこのように書きます:ComboBoxItemTemplateではなくBindingにテンプレートを適用するStaticResource 4.Use

<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <ComboBox ItemTemplate="xxx" ItemsSource="xxx"/> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

ComboBoxItemsSource 5.For

<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <ComboBox ItemTemplate="{StaticResource iconTemplate}" ItemsSource="xxx"/> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

あなたは、ウィンドウのRelativeSourceを使用して、あなたはiconListleftIconListrightIconListどちらに特異的に結合する必要があります。 2つはウィンドウに属していないからです。

ItemsSource="{Binding iconList, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" 

6.達成する必要があります。

関連する問題