开发者

Silverlight ComboBox in DataGrid Binding SelectedItem problem

开发者 https://www.devze.com 2023-04-08 10:45 出处:网络
I have a combobox in datagrid.I use Silverlight 4.0 and MVVM. My code works fine,unless when I removed a record from datagrid and add another one, the SelectedValue binding for combobox in added row d

I have a combobox in datagrid.I use Silverlight 4.0 and MVVM. My code works fine,unless when I removed a record from datagrid and add another one, the SelectedValue binding for combobox in added row doesnt work.

 <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>
开发者_开发百科

Thanks


Found this piece of code on some site, it helped me in a similar Situation:

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);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消