【问题标题】:Silverlight ComboBox in DataGrid Binding SelectedItem problemDataGrid 中的 Silverlight ComboBox 绑定 SelectedItem 问题
【发布时间】:2011-11-24 14:22:15
【问题描述】:

我在 datagrid 中有一个组合框。我使用 Silverlight 4.0 和 MVVM。 我的代码工作正常,除非我从数据网格中删除一条记录并添加另一条记录,否则添加行中组合框的 SelectedValue 绑定不起作用。

 <sdk:DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Items, Mode=TwoWay}" Name="dataGrid2" >
        <sdk:DataGrid.Columns>                
            <sdk:DataGridTemplateColumn Width="50*">
                <sdk:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding  Path=Products, Mode=OneWay}"    
                                  SelectedValue="{Binding Path=ProductId,Mode=TwoWay}"
                                  DisplayMemberPath="ProductTitle" 
                                  SelectedValuePath="ProductId"/>
                    </DataTemplate>
                </sdk:DataGridTemplateColumn.CellEditingTemplate>
            </sdk:DataGridTemplateColumn>
        </sdk:DataGrid.Columns>          
    </sdk:DataGrid>

谢谢

【问题讨论】:

    标签: silverlight-4.0 mvvm datagrid combobox


    【解决方案1】:

    在某个网站上找到了这段代码,它在类似的情况下帮助了我:

    public class ComboBoxEx : ComboBox
    {
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            var bindingExpression = GetBindingExpression(SelectedValueProperty);
    
            base.OnItemsChanged(e);
    
            if (bindingExpression != null)
            {
                var binding = bindingExpression.ParentBinding;
                SetBinding(SelectedValueProperty, bindingExpression.ParentBinding);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多