2015-09-16 166 views
1

我正面臨與WPF XAML中的DataGrid內部的Combobox數據綁定的一些問題。GridView Combobox DataBinding WPF

我有Signal類有ObservableCollection。

public struct RawVal 
{ 
    public string name; //Name of the Value 
    public int value; // Value 
} 


public class Signal 
{ 
    public string Name { get; set; } 
    public Int32 Value { get; set; } 
    public ObservableCollection<RawVal> rawValue { get; set; } 
}; 

而在Stettings窗口有

public partial class Settings : Window 
{  
    public ObservableCollection<Signal> tempSigList { get; set; } 
    public ObservableCollection<RawVal> tempRawVal { get; set; } 
    ......... 
    ......... 
this.grdSignal.ItemsSource = tempSigList; 

}

我的XAML看起來像這樣:

<DataGrid ItemsSource="{Binding}" Name="grdSignal"> 
<DataGrid.Columns>  
    <DataGridTemplateColumn Header=" RAW Value "> 
     <DataGridTemplateColumn.CellTemplate> 
      <DataTemplate> 
       <ComboBox ItemsSource="{Binding tempRawVal }" SelectedItem="Binding name" /> 
      </DataTemplate> 
     </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

但沒有任何結果ㅠㅠㅠ 我已經嘗試了很多例子在stackoverflow但沒有任何結果。 歡迎任何建議或解決方案。 謝謝

+0

謝謝兄弟。看來還有其他問題。其他項目中的組合框也不顯示任何項目。它下降,但沒有任何關係。我正在使用.NET Framework 4. – Mamurbek

回答

0

這裏:

窗口:

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    tempSigList = new ObservableCollection<Signal>(); 
    tempSigList.Add(new Signal { Name = "Name 1", Value = 1, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 1", value = 1 }, new RawVal { name = "combo 2", value = 2 } } }); 
    tempSigList.Add(new Signal { Name = "Name 2", Value = 2, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 3", value = 3 }, new RawVal { name = "combo 4", value = 4 } } }); 
    tempSigList.Add(new Signal { Name = "Name 3", Value = 3, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 5", value = 5 }, new RawVal { name = "combo 6", value = 6 } } }); 
    tempSigList.Add(new Signal { Name = "Name 4", Value = 4, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 7", value = 7 }, new RawVal { name = "combo 8", value = 8 } } }); 
    tempSigList.Add(new Signal { Name = "Name 5", Value = 5, rawValue = new ObservableCollection<RawVal> { new RawVal { name = "combo 9", value = 9 }, new RawVal { name = "combo 10", value = 10 } } }); 

    dataGrid1.ItemsSource = tempSigList; 
} 

XAML:

<DataGrid x:Name="dataGrid1" ItemsSource="{Binding}"> 
    <DataGrid.Columns> 
     <DataGridTemplateColumn Header="RAW Value"> 
      <DataGridTemplateColumn.CellTemplate> 
       <DataTemplate> 
        <ComboBox ItemsSource="{Binding rawValue}" SelectedItem="{Binding name}" DisplayMemberPath="name" /> 
       </DataTemplate> 
      </DataGridTemplateColumn.CellTemplate> 
     </DataGridTemplateColumn> 
    </DataGrid.Columns> 
</DataGrid> 

enter image description here

我用AutoGenerateColumns=true。您可以製作它false並且只有您的ComboBox列才能顯示。

enter image description here

+0

謝謝布羅,它運行良好。問題是公共結構RawVal成員沒有getter和setter。 – Mamurbek

+0

是啊,我用了一個公共財產類,結果忘了告訴你...... O: – jsanalytics

+0

非常感謝你,先生! – jsanalytics

0

首先,結合與性質,而不是領域作品在你的類RawVal。

表示使用3列中的數據,因爲它們應該是:

public partial class Win32599087 : Window 
    { 
     public ObservableCollection<Signal> tempSigList { get; set; } 
     public ObservableCollection<RawVal> tempRawVal { get; set; } 

     public Win32599087() 
     { 
      InitializeComponent(); 

      tempRawVal = new ObservableCollection<RawVal>() { 
       new RawVal(){ Name="RawName1", Value=1}, 
       new RawVal(){ Name="RawName2", Value=2}, 
       new RawVal(){ Name="RawName3", Value=3}, 
       new RawVal(){ Name="RawName4", Value=4} 

      }; 

      tempSigList = new ObservableCollection<Signal>() {    
       new Signal(){Name="signal1", Value = 1, RawValues = tempRawVal} 
      }; 

      SignalGrid.DataContext = tempSigList; 
     } 
    } 

//////////////

<DataGrid x:Name="SignalGrid" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}"> 
     <DataGrid.Columns>     

      <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
      <DataGridTextColumn Header="Value" Binding="{Binding Value}"/> 
      <DataGridComboBoxColumn Header="RawValues"> 
      <DataGridComboBoxColumn.EditingElementStyle> 
       <Style TargetType="ComboBox"> 
        <Setter Property="ItemsSource" Value="{Binding Path=RawValues}" /> 
        <Setter Property="DisplayMemberPath" Value="Name" /> 
        <Setter Property="SelectedValuePath" Value="Value" /> 
       </Style> 
       </DataGridComboBoxColumn.EditingElementStyle> 
      </DataGridComboBoxColumn> 

     </DataGrid.Columns> 
    </DataGrid> 

/////// With templatecolumn:

<DataGrid x:Name="SignalGridWithTemplates" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}"> 
      <DataGrid.Columns> 

       <DataGridTextColumn Header="Name" Binding="{Binding Name}"/> 
       <DataGridTextColumn Header="Value" Binding="{Binding Value}"/> 
       <DataGridTemplateColumn Header="RawValues"> 
        <DataGridTemplateColumn.CellTemplate> 
         <DataTemplate> 
          <StackPanel> 
           <ComboBox ItemsSource="{Binding RawValues}"> 
            <ComboBox.ItemTemplate> 
             <DataTemplate> 
              <StackPanel Orientation="Horizontal"> 
               <TextBlock Text="{Binding Name}" Background="AliceBlue"/> 
               <TextBlock Text=" | "/> 
               <TextBlock Text="{Binding Value}" Background="Azure"/> 
              </StackPanel> 
             </DataTemplate> 
            </ComboBox.ItemTemplate> 
           </ComboBox> 
          </StackPanel> 
         </DataTemplate> 
        </DataGridTemplateColumn.CellTemplate> 
       </DataGridTemplateColumn> 

      </DataGrid.Columns> 
     </DataGrid> 
+0

閱讀此博客文章:[DataGrid佈局](http://blogs.msdn.com/b/vinsibal/archive/2008/08/14/wpf-datagrid-dissecting-the-visual-layout.aspx) – AnjumSKhan