簡體   English   中英

如何將自定義類集合綁定到WPF DataGrid組合框(多選)

[英]how to bind custom class collections to wpf datagrid combobox (multiselected)

請幫助我弄清楚如何將自定義類集合綁定到datagrid組合框。 我的自定義課程是

class Test: INotifyPropertyChanged
{
    public String Name { get; set; }
    public UserAvailableValue SelectedAvailableValue { get; set; }
    public ObservableCollection<UserAvailableValue> AvailableValues { get;  set; }
    public ObservableCollection<String> DefaultValues { get;  set; }
    public String SelectedValue { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class UserAvailableValue 
{
    public Object Value { get; set; }
    public Object Label { get; set; }
}

從代碼后面,我正在設置DataGrid Datacontext ig

   ObservableCollection<Test> UIParams = new ObservableCollection<Test>();
   // code to fill UIParams collection
   dgReportparameters.DataContext = UIParams; 

   //XAML Code
   <DataGrid Name="dgReportparameters" ItemsSource="{Binding}"
               AutoGenerateColumns="False">
     <DataGrid.Columns>
     <DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
     <DataGridComboBoxColumn Header="Available Values" SelectedItemBinding=
      "{Binding SelectedAvailableValue, UpdateSourceTrigger=PropertyChanged}" 
       DisplayMemberPath="Label">
        <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
        <Setter Property="ItemsSource" Value="{Binding Path=AvailableValues, 
         RelativeSource={RelativeSource AncestorType=Window}}" />
         </Style>
        </DataGridComboBoxColumn.ElementStyle>
      </DataGridComboBoxColumn>
   <DataGridTextColumn Header="Default Values" Binding="{Binding SelectedValue}"/>
   <DataGridCheckBoxColumn Header="Nullable" Binding="{Binding IsNullable}"/>
 </DataGrid.Columns>
</DataGrid>

除了DataGridComboBoxColumn以外,其他列均顯示正確的值.DataGridComboBoxColumn則顯示空白列。 UIParams集合具有多個參數,而每個參數都有名稱,可用值和一個默認值。 我想在datagrid中顯示參數,並讓用戶從“可用”列組合框中選擇一個/多個值。 每個參數都有其自己的一組可用值。 我發現的大多數示例在下拉列表中都有Common集合,但在我的情況下,datagrid的每一行都有不同的可用值。

請幫助我在datagrid中使用組合框。

提前致謝。

您的組合框樣式錯誤。 ItemsSource設置為Window,而在您的模型中,您不需要設置AncestorType。

只需{Binding}就足夠了。 實際上,您只需要定義Combobox的樣式,就不需要ItemsSource Setter。 僅在將ItemsSource修改為其他內容時才需要它。 就您而言,不是。

編輯:看一下這個例子: WPF DataGrid中ComboBoxColumn的ItemsItemSource綁定我不理解您在每個數據項中都具有AvailableValues集合的要求,而您在頂層具有它,如下所示。

public class MyClass
{
public List<Test> Items{get;set;}
public List<AvailableValue> AvailableValues { get;set;}
}

我還注意到您已經實現了INotifyPropertyChanged接口,並且沒有在每個屬性集上引發change事件。

我建議您在開始使用WPF和INotifyPropertyChanged接口之前先學習一些基礎知識。

這對我有幫助。

    <DataGridComboBoxColumn Header="Available Values" SelectedItemBinding=
                 "{Binding SelectedAvailableValue, UpdateSourceTrigger=PropertyChanged}"                     
                 DisplayMemberPath="Label">
            <DataGridComboBoxColumn.EditingElementStyle>
               <Style TargetType="ComboBox">
                  <Setter Property="ItemsSource" Value="{Binding Path=AvailableValues}" />
               </Style>
            </DataGridComboBoxColumn.EditingElementStyle>
  </DataGridComboBoxColumn> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM