wpf 工具包、数据网格、组合框列

发布于 2024-08-07 18:54:49 字数 196 浏览 2 评论 0 原文

在数据网格中,我有两个 DataGridComboBoxColumns。其中一列的项目应取决于另一列中选择的内容。用于建模的底层集合是一个 dictionary>。 我应该如何实现这个?我似乎无法连接到列上的任何相关事件,并且我找不到任何支持此功能的数据绑定方案。

In a datagrid I have two DataGridComboBoxColumns. The items of one of these columns should depend on what is selected in the other column. The underlying collection used to model this is a dictionary<string,List<string>>. How should i go about implementing this? I can't seem to hook up to any relevant events on the columns, and I cant find any databinding scenarios that support this..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎的声音 2024-08-14 18:54:49

不久前我遇到了同样的情况,并像这样修复了它:

  public class DataItem : INotifyPropertyChanged {
  ...

  public List<SomeObject> DisplayableComboBoxItems { 
    get; set;
}

private static Dictionary<int, List<SomeObject>> myDict;    

public Dictionary<int, List<SomeObject>> MyDict {
    get {
        if (myDict == null) {
            myDict = GetYourDataFromSomewhere();
        }
        return myDict;
    }
}

public int TypeId {
    get { return typeId; }
    set {
        if (value == typeId) return;
        typeId = value;
        RaisePropertyChanged("TypeId");
    }
}

public int TypeSetId {
    get { return typeSetId; }
    set {
        if (typeSetId == value) return;         
        typeSetId = value;
        RaisePropertyChanged("TypeSetId");
        DisplayableComboBoxItems = MyDict[typeSetId];
        RaisePropertyChanged("DisplayableComboBoxItems");
        TypeId = 0;                
    }
}
...
}

DataItem 是绑定到 DataRow 的对象。
这只是代码的一个小模型。基本上,每当 TypeSet 发生更改时,我都需要显示新的类型列表。我只使用了一个静态列表,在这个例子中我使用了一个字典。
通过此设置,您可以将组合框 ItemsSource 绑定到“DisplayableComboBoxItems”,并将 SelectedValue 绑定到“TypeId”。
您将需要其他属性来显示正确的文本而不是 TypeId。
这样做的缺点是,当您有 1000 多个项目时,所有项目都会有相同的列表。然而我的情况并非如此(DataGrid 显示最多 50 个项目)。

我希望这足够清楚,并且可以帮助您朝着正确的方向前进!

干杯!
罗尔

I had the same scenario a while back and fixed it like this:

  public class DataItem : INotifyPropertyChanged {
  ...

  public List<SomeObject> DisplayableComboBoxItems { 
    get; set;
}

private static Dictionary<int, List<SomeObject>> myDict;    

public Dictionary<int, List<SomeObject>> MyDict {
    get {
        if (myDict == null) {
            myDict = GetYourDataFromSomewhere();
        }
        return myDict;
    }
}

public int TypeId {
    get { return typeId; }
    set {
        if (value == typeId) return;
        typeId = value;
        RaisePropertyChanged("TypeId");
    }
}

public int TypeSetId {
    get { return typeSetId; }
    set {
        if (typeSetId == value) return;         
        typeSetId = value;
        RaisePropertyChanged("TypeSetId");
        DisplayableComboBoxItems = MyDict[typeSetId];
        RaisePropertyChanged("DisplayableComboBoxItems");
        TypeId = 0;                
    }
}
...
}

DataItem is the object that gets bound to a DataRow.
This is just a small mock-up of the code. Basically, whenever the TypeSet changes, I needed a new list of Types to be displayed. I used just a static list, in this example i used a dictionary.
With this setup you can bind you combobox ItemsSource to the 'DisplayableComboBoxItems', and your SelectedValue to "TypeId".
You're gonna need other properties to display the correct text instead of the TypeId.
The downside of this is that when you have 1000+ items, you'll have that same list for all items. This wasn't however the case with me (DataGrid showed max 50 items).

I hope this is clear enough and that it helps you in the right direction!

cheers!
Roel

尘世孤行 2024-08-14 18:54:49

我没有对第二列使用 DataGridComboBoxColumn,而是使用带有嵌入式 Combobox 的 DataGridTemplateColumn。对于 itemsource,我定义了一个转换器: string ->列表<字符串>。转换器将另一个DataGridComboBox(绑定到Navn)的选定项的值转换为List,这只是一个字典查找。

就像这样:

<my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  SelectedItem="{Binding Værdi}" 
                                    ItemsSource="{Binding Navn,  Converter={StaticResource dimensionToValues}}"
                                   > 
                        </ComboBox>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>

Instead of using a DataGridComboBoxColumn for the second column, I went with a DataGridTemplateColumn with an embedded Combobox. For the itemsource i defined a converter: string -> List<string>. The converter translates the value of the selecteditem of the other DataGridComboBox (which is bound to Navn) into List<string>, this is just a dictionary lookup.

Like so:

<my:DataGridTemplateColumn>
                <my:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox  SelectedItem="{Binding Værdi}" 
                                    ItemsSource="{Binding Navn,  Converter={StaticResource dimensionToValues}}"
                                   > 
                        </ComboBox>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
            </my:DataGridTemplateColumn>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文