WPF DataGrid
存在问题,这让我发疯。
让我们考虑一下这个视图模型:
public class ViewModel : INotifyPropertyChanged
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsSelected
{
get { return isSelected; }
set
{
System.Diagnostics.Debug.WriteLine("{0}'s IsSelected new value is: {1}", Name, value);
if (isSelected != value)
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
}
private bool isSelected;
// INPC implementation
}
......这个XAML:
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding}" IsReadOnly="True" AutoGenerateColumns="False"
SelectionMode="Extended" SelectionUnit="FullRow">
<DataGrid.ItemContainerStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="Is selected" Binding="{Binding IsSelected}"/>
<DataGridTextColumn Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
...和这个代码隐藏:
public partial class MainWindow : Window
{
private IList<ViewModel> GenerateViewModels()
{
var viewModels = new List<ViewModel>();
for (var i = 0; i < 100; i++)
{
viewModels.Add(new ViewModel
{
Id = i,
Name = string.Format("Item {0}", i)
});
}
return viewModels;
}
public MainWindow()
{
InitializeComponent();
DataContext = GenerateViewModels();
}
}
案例1。
调试输出:
Item 0's IsSelected new value is: True
Item 0's IsSelected new value is: False
Item 10's IsSelected new value is: True
Item 10's IsSelected new value is: False
案例2。
调试输出:
Item 0's IsSelected new value is: True
Item 1's IsSelected new value is: True
Item 2's IsSelected new value is: True
Item 2's IsSelected new value is: False
Item 10's IsSelected new value is: True
当选择模式延长时,问题是再现。当它是单身时,一切正常。
问题:
我错过了什么吗?
2.有人知道解决方法吗?
更新
我已为网格添加了SelectionChanged
事件处理程序:
private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null)
{
foreach (var item in e.AddedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is added to selection.", item.Id);
}
}
if (e.RemovedItems != null)
{
foreach (var item in e.RemovedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is removed from selection.", item.Id);
}
}
e.Handled = true;
}
调试输出显示,SelectedItems
集合已正确更新 。例如,对于第一种情况输出将是:
Item 0's IsSelected new value is: True
** Item 0 is added to selection.
Item 0's IsSelected new value is: False
Item 10's IsSelected new value is: True
** Item 10 is added to selection.
** Item 0 is removed from selection.
Item 10's IsSelected new value is: False
** Item 0 is added to selection.
** Item 10 is removed from selection.
但绑定数据属性IsSelected
未更新!
答案 0 :(得分:0)
至少找到一个解决方法,它与问题的更新有关
让我们稍微修改SelectionChanged
事件处理程序:
private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null)
{
foreach (var item in e.AddedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is added to selection.", item.Id);
if (!item.IsSelected)
{
// if bound data item still isn't selected, fix this
item.IsSelected = true;
}
}
}
if (e.RemovedItems != null)
{
foreach (var item in e.RemovedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is removed from selection.", item.Id);
if (item.IsSelected)
{
// if bound data item still is selected, fix this
item.IsSelected = false;
}
}
}
e.Handled = true;
}
但它绝对是DataGrid
中的一个错误,不是吗?
答案 1 :(得分:0)
我试过了,我认为你需要将isSelected设置为false以适应这两种情况。这个对我有用。但感谢最初的解决方案!它对我帮助很大。
private void MyGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null)
{
foreach (var item in e.AddedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is added to selection.", item.Id);
if (!item.IsSelected)
{
// if bound data item still isn't selected, fix this
item.IsSelected = false;
}
}
}
if (e.RemovedItems != null)
{
foreach (var item in e.RemovedItems.Cast<ViewModel>())
{
System.Diagnostics.Debug.WriteLine("** Item {0} is removed from selection.", item.Id);
if (item.IsSelected)
{
// if bound data item still is selected, fix this
item.IsSelected = false;
}
}
}
e.Handled = true;
}