在wpf的数据网格中插入组合框
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"
/>
现在的问题是,我可以在加载时看到 comobox 中的名称...当我单击组合框时,下拉列表会显示每个项目的 MainDataContext.Category...
当我单击任何下拉列表时项目..选择正确的值并..在组合框中显示我...
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"
/>
Now the thing is i can see the name in the comobox when it loads... when i click on the combobox the dropdown shows me MainDataContext.Category for each and every item...
when i click on any of the drop down items.. its select the correct value and.. showing me in the combobox...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该自己设置categoriesComboBox.ItemsSource = cat;,请尝试使用数据上下文进行设置:
categoriesComboBox.DataContext = cat
。不管怎样,您已经在 XAML 中获得了所需的绑定。希望这是您的实际问题(因为我无法从您的问题中理解问题所在)。
编辑:从您的图像中,您需要为您的类型
MainContext.Category
指定一个DataTemplate
。如果没有它,则使用默认模板,该模板仅输出字符串表示形式。You should not set
categoriesComboBox.ItemsSource = cat;
yourself, try doing it with the data context:categoriesComboBox.DataContext = cat
. Anyway, you've got the needed bindings in your XAML.Hope that this is your actual problem (since I didn't manage to understand what the problem is from your question).
Edit: from your images, you need to specify a
DataTemplate
for your typeMainContext.Category
. Without it, the default template is used, which just outputs the string representation.