綁定到CollectionView源時,所有行顯示最初顯示的空白值,然後當我更改任何組合框的選擇時,所有組合框都設置爲相同的值。當我直接綁定到ObservableCollection的源集合時,它可以正常工作。我想使用一個CollectionViewSource,這樣我就可以利用它的排序功能等。下面是一些代碼,它使用一個綁定到CollectionViewSource的列和一個直接綁定到底層ObservableCollection的方法來解決問題。我使用VS 2015將數據網格組合框列綁定到CollectionViewSource時的奇怪行爲
視圖模型:
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();
}
}
你試過用'Collec tionViewSource'在XAML?例如' ',然後在您的'DataGrid'中'。 –
XAMlMAX