数据绑定在数据网格中的wpf组合框中

时间:2013-03-28 17:23:47

标签: c# wpf binding datagrid combobox

我遇到一个与ComboBox DataGrid相关的问题。数据具有绑定性,但绑定后未显示在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;
    }
}

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

1 个答案:

答案 0 :(得分:0)

您是否尝试将Binding更改为StaticResource,类似

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