當使用型號爲DataContext的一個WPF窗口,DataGrid的SelectionChanged
事件不會被調用後窗口加載這就是爲什麼該行從來沒有突出顯示,你只能看到第一個,直到與部分重點排。可能有更優雅的方式,但這裏有一個解決方法。
在窗口的加載事件或DataGrid的加載事件,重置的SelectedItem綁定:
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(OnLoaded);
}
// could also be placed in the DataGrid's loaded event handler
private void OnLoaded(object sender, RoutedEventArgs e)
{
if(dataGrid != null && Model.SelectedItem != null)
{
var selected = Model.SelectedItem;
Model.SelectedItem = null;
Model.SelectedItem = selected;
}
}
下面是一個完整的工作樣本。
XAML
<Window x:Class="WpfDataGridHighlightOnLoad.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:model="clr-namespace:WpfDataGridHighlightOnLoad"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<model:MainWindowModel x:Name="Model" />
</Window.DataContext>
<Grid>
<DataGrid AutoGenerateColumns="True" SelectionMode="Single"
HorizontalAlignment="Stretch"
Name="dataGrid"
VerticalAlignment="Top"
ItemsSource="{Binding ItemList}"
SelectedItem="{Binding SelectedItem}">
</DataGrid>
<Button Content="Cycle Selection" Click="OnCycleClick"
Height="23"
HorizontalAlignment="Right"
Name="button1"
VerticalAlignment="Bottom" Width="125" />
<Button Content="Reset Grid" Click="OnResetClick"
Height="23"
HorizontalAlignment="Left"
Name="button2"
VerticalAlignment="Bottom" Width="125" />
</Grid>
</Window>
代碼隱藏
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace WpfDataGridHighlightOnLoad
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(OnLoaded);
}
// could also be placed in the DataGrid's loaded event handler
private void OnLoaded(object sender, RoutedEventArgs e)
{
if(dataGrid != null && Model.SelectedItem != null)
{
var selected = Model.SelectedItem;
Model.SelectedItem = null;
Model.SelectedItem = selected;
}
}
private void OnCycleClick(object sender, RoutedEventArgs e)
{
int index = Model.ItemList.IndexOf(Model.SelectedItem);
index = index == Model.ItemList.Count - 1 ? 0 : index + 1;
Model.SelectedItem = Model.ItemList[index];
}
private void OnResetClick(object sender, RoutedEventArgs e)
{
Model.Reset();
}
}
public class MainWindowModel : INotifyPropertyChanged
{
public MainWindowModel()
{
Reset();
}
public void Reset()
{
ItemList = new List<Person>
{
new Person("Joe", 20),
new Person("John", 30),
new Person("Jane", 40),
new Person("Jill", 50),
new Person("Fido", 7),
};
SelectedItem = ItemList[2];
}
private Person _selectedItem;
public Person SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
NotifyPropertyChanged("SelectedItem");
}
}
private List<Person> _itemList;
public List<Person> ItemList
{
get { return _itemList; }
set
{
_itemList = value;
NotifyPropertyChanged("ItemList");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public Person(string name, int age)
{
Name = name;
Age = age;
}
public override string ToString()
{
return Name;
}
}
}
沒有''上UIElement' – 2011-03-08 15:07:12
Loaded'事件'DataGrid'從'Controls'這確實有一個'Loaded'事件繼承// msdn.microsoft.com/en-us/library/system.windows.controls.datagrid_events.aspx) – 2011-03-08 15:41:12
@Smurf - 重置SelecteItem已經解決了我一直試圖瞭解的問題幾天。謝謝!!! – GilShalit 2011-08-30 18:23:55