【问题标题】:Inserting a combobox in a datagrid in wpf在 wpf 的数据网格中插入组合框
【发布时间】:2011-12-05 12:56:09
【问题描述】:

C#

 MainDataContext db = new MainDataContext();
var cat = from p in db.Categories
                      select p;
categoriesComboBox.ItemsSource = cat;
            categoriesComboBox.SelectedValuePath = "ID";
            categoriesComboBox.DisplayMemberPath = "CatName";

XAML

<Custom:DataGridComboBoxColumn                                    
Width="1*"                                  x:Name="categoriesComboBox" 
Header="Category"
ItemsSource="{Binding}"
SelectedValueBinding="{Binding Path=Category}"
SelectedValuePath="ID"
DisplayMemberPath="CatName"
/>

现在的事情是我可以在加载时在组合框中看到名称...当我单击组合框时,下拉菜单会显示每个项目的 MainDataContext.Category...

当我点击任何下拉项目时..它选择正确的值并..在组合框中显示我...

【问题讨论】:

    标签: wpf wpfdatagrid wpftoolkit datagridcomboboxcolumn


    【解决方案1】:

    您不应该自己设置categoriesComboBox.ItemsSource = cat;,请尝试使用数据上下文:categoriesComboBox.DataContext = cat。无论如何,您的 XAML 中已经有了所需的绑定。

    希望这是您的实际问题(因为我无法从您的问题中理解问题所在)。


    编辑:从您的图像中,您需要为您的类型MainContext.Category 指定一个DataTemplate。没有它,使用默认模板,它只输出字符串表示。

    【讨论】:

    • 实际上我做不到,请给我一个简单的例子。 :)
    • @siddhartha:看here,那里有一些例子。或here.
    猜你喜欢
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 2017-05-17
    • 2013-03-19
    • 2018-06-27
    • 2011-08-13
    • 2011-02-05
    • 2014-01-08
    相关资源
    最近更新 更多