将数据网格组合框列绑定到CollectionViewSource时的奇怪行为

时间:2017-07-31 12:59:00

标签: c# wpf

当绑定到CollectionView源时,所有行显示最初显示空白值,然后当我更改任何组合框上的选择时,所有组合框都设置为相同的值。当我直接绑定到一个ObservableCollection的源集合时,它工作正常。我想使用CollectionViewSource,因此我可以利用它的排序功能等。这里有一些代码可以解决问题,一个列绑定到CollectionViewSource,另一个绑定到底层的ObservableCollection。我正在使用VS 2015。

视图模型:

public class GridItem
{
    public string Name { get; set; }
    public int CompanyID { get; set; }
    public int CompanyID2 { get; set; }
}

public class CompanyItem
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class ViewModel
{
    public ViewModel()
    {
        GridItems = new ObservableCollection<GridItem>() {
            new GridItem() { Name = "Jim", CompanyID = 1, CompanyID2 = 1 },
            new GridItem() { Name = "Ed", CompanyID = 2, CompanyID2 = 2 },
            new GridItem() { Name = "Dave", CompanyID = 3, CompanyID2 = 3 },
            new GridItem() { Name = "Bruce", CompanyID = 4, CompanyID2 = 4 },
            new GridItem() { Name = "Rob", CompanyID = 5, CompanyID2 = 5 }
        };

        CompanyItems = new ObservableCollection<CompanyItem>() {
            new CompanyItem() { ID = 1, Name = "Company 1" },
            new CompanyItem() { ID = 2, Name = "Company 2" },
            new CompanyItem() { ID = 3, Name = "Company 3" },
            new CompanyItem() { ID = 4, Name = "Company 4" },
            new CompanyItem() { ID = 5, Name = "Company 5" },
            new CompanyItem() { ID = 6, Name = "Company 6" },
            new CompanyItem() { ID = 7, Name = "Company 7" },
        };

        CompanyItemsViewSource = new CollectionViewSource();
        CompanyItemsViewSource.Source = CompanyItems;
    }

    public ObservableCollection<GridItem> GridItems { get; set; }
    public ObservableCollection<CompanyItem> CompanyItems { get; set; }
    public CollectionViewSource CompanyItemsViewSource { get; set; }
}

窗口:

<Window x:Class="DataGridTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding GridItems}" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Name}" Header="Name"/>

                <DataGridComboBoxColumn 
                    SelectedValueBinding="{Binding CompanyID}"  DisplayMemberPath="Name"  SelectedValuePath="ID" Header="Company (View Source)">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItemsViewSource.View, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItemsViewSource.View, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>

                <DataGridComboBoxColumn 
                    SelectedValueBinding="{Binding CompanyID2}"  DisplayMemberPath="Name"  SelectedValuePath="ID" Header="Company">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="{x:Type ComboBox}">
                            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItems, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

数据上下文在应用启动时设置:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();
        ViewModel viewModel = new ViewModel();

        window.DataContext = viewModel;
        window.Show();
    }
}

1 个答案:

答案 0 :(得分:1)

您将同一CollectionViewSource的相同视图绑定到所有不同的行。当您更改其中一行时,您正在操作基础视图,该视图将在其他行上进行复制。

您需要为不同的行设置不同的集合视图。要仅使用一个属性执行此操作,您可以在每次调用get访问者时创建新视图。

向ViewModel添加类型为ICollectionView的新属性,如下所示:

public ICollectionView CompanyItemCollectionView
{
    get
    {            
        return new CollectionViewSource { Source = CompanyItems }.View;
    }
} 

将此属性绑定到DataGridComboBoxColumn元素的ItemsSource:

<DataGridComboBoxColumn 
SelectedValueBinding="{Binding CompanyID}"  DisplayMemberPath="Name"  SelectedValuePath="ID" Header="Company (ICollectionView)">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="{x:Type ComboBox}">
           <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItemCollectionView, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.CompanyItemCollectionView, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

您也可以在get访问器中设置过滤器和排序顺序:

public ICollectionView CompanyItemCollectionView
{
    get
    {
        ICollectionView view = new CollectionViewSource { Source = CompanyItems }.View;
        view.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Descending)); //example                
        return view;
    }
}