【问题标题】:data binding in wpf combo box in data grid数据网格中 wpf 组合框中的数据绑定
【发布时间】:2013-03-19 06:28:13
【问题描述】:

我遇到了一个与DataGrid 中的ComboBox 相关的问题。数据正在绑定,但绑定后未显示在 ComboBox 中。我的代码如下:

<my:DataGridTemplateColumn Header="UsgSrc">
    <my:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Name="cbUsgSrc"  
                      ItemsSource="{Binding Source={StaticResource UsgSrcUOMS}}" 
                      SelectedValue="{Binding Path=UsgSrc}" 
                      SelectedValuePath="UtType" 
                      DisplayMemberPath="UtType">
            </ComboBox>
        </DataTemplate>
    </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

静态资源代码:

UsgSrcUOMS.ObjectDataProvider UsageSrcUOMS = null;
UsageSrcUOMS = (ObjectDataProvider)FindResource("UsgSrcUOMS");
UsageSrcUOMS.ObjectInstance = objUtView;
Microsoft.Windows.Controls.DataGridCell cell = obj.GetCell(dgMtrHdr, J, 11);

if (cell != null)
{
    ContentPresenter panel = cell.Content as ContentPresenter; 
    if (panel != null)
    {
       ComboBox cmbUsUtilit = obj.GetVisualChild<ComboBox>(panel);
       cmbUsUtilit.IsEnabled = true;
       if(objUtView!=null) 
         cmbUsUtilit.ItemsSource = objUtView;cmbUsUtilit.SelectedIndex=2;
    }
}

这是什么原因。请帮我解决我的问题。

【问题讨论】:

    标签: c# wpf binding datagrid combobox


    【解决方案1】:

    您是否尝试过将 Binding 更改为 StaticResource 之类的东西

    <ComboBox Name="cbUsgSrc"  
              ItemsSource="{StaticResource UsgSrcUOMS}" 
              SelectedValue="{Binding Path=UsgSrc}" 
              SelectedValuePath="UtType" 
              DisplayMemberPath="UtType">
    </ComboBox>
    

    【讨论】:

    • 嗨,Sine,刚才我按照您所说的进行了尝试。但是我在后面的代码中收到以下错误。 “System.Windows.Data.ObjectDataProvider”不是属性“ItemsSource”的有效值。
    • 能否分享一下定义StaticResource UsgSrcUOMS的代码
    • 嗨 Sine,感谢您的回复。在后面的代码中,我将数据分配给 UsgSrcUOMS.ObjectDataProvider UsageSrcUOMS = null;UsageSrcUOMS = (ObjectDataProvider)FindResource("UsgSrcUOMS"); UsageSrcUOMS.ObjectInstance = objUtView;Microsoft.Windows.Controls.DataGridCell 单元格 = obj.GetCell(dgMtrHdr, J, 11); if (cell != null){ContentPresenter panel = cell.Content as ContentPresenter; if (panel != null){ComboBox cmbUsUtilit = obj.GetVisualChild(panel); cmbUsUtilit.IsEnabled = true;if(objUtView!=null) cmbUsUtilit.ItemsSource = objUtView;cmbUsUtilit.SelectedIndex=2;}}
    • 您能否使用 objectdataprovider 的 xaml 代码更新您的问题!
    猜你喜欢
    • 2017-05-17
    • 2018-05-21
    • 2014-01-08
    • 2012-10-01
    • 2011-09-09
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多