【问题标题】:Binding a combobox selected item content to an element将组合框选定项内容绑定到元素
【发布时间】:2013-04-18 01:03:02
【问题描述】:

我正在使用 WPF 使用实体框架和数据绑定来管理数据库中的 PRODUCT 和 BRAND。 PRODUCT 包含 BrandID 属性。为了在数据网格中显示品牌名称而不是 ID,我使用了这样的 DataGridComboBoxColumn:

C#

gridProducts.DataContext = this.DbContext.Products;
brandColumn.ItemsSource = this.DbContext.Brands;

XAML

<DataGrid Name="gridProducts" IsSynchronizedWithCurrentItem="True"
          RowEditEnding="dg_RowEditEnding" ItemsSource="{Binding}" >
  <DataGrid.Columns>
    ...
    <DataGridComboBoxColumn Header="BRAND" x:Name="brandColumn" DisplayMemberPath="BrandName" 
       SelectedValuePath="ID" SelectedValueBinding="{Binding Path=BrandID}" />
 </DataGrid.Columns>
<DataGrid>

它工作正常。但现在我需要将组合框选定值绑定到文本框文本中。我试过这个:

XAML

    <TextBox Name="brand" Text="{Binding Path=SelectedValue.Content, 
      ElementName=brandColumn}"/>

但它不起作用。关于如何在文本框中绑定组合框选定值(BrandName - 而不是 BrandID)的任何想法?

【问题讨论】:

标签: c# wpf data-binding datagrid combobox


【解决方案1】:

不要在 xaml 中绑定它,而是在实体/视图模型中创建一个公共属性并将其绑定到该属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-20
    • 2013-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多