你的问题是,所选择的行/细胞不被聚焦的焦点仍然在你的ListView项。你可以做的是设计xaml中的DataGridCell元素。下面是证明这一小段代码:
<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<DataGrid ItemsSource="{Binding Tests}"
SelectedItem="{Binding GridSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding SelectedGridIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
</DataGrid>
<Button Command="{Binding ChangeSelectedItemCommand}"
Content="Change Grid Selected item"
Grid.Column="1"
VerticalAlignment="Top"/>
</Grid>
,这里的视图模型部分:
public class MainWindowViewModel : INotifyPropertyChanged
{
#region Private members
private List<TestClass> _tests;
private TestClass _gridSelectedItem;
private ICommand _changeSelectedItemCommand;
private int _selectedGridIndex;
#endregion
#region Constructor
public MainWindowViewModel()
{
Tests = new List<TestClass>();
for (int i = 0; i < 25; i++)
{
TestClass testClass= new TestClass {Name = "Name " + i, Title = "Title" + i};
Tests.Add(testClass);
}
}
#endregion
#region Public properties
public List<TestClass> Tests
{
get { return _tests; }
set
{
_tests = value;
OnPropertyChanged("Tests");
}
}
public TestClass GridSelectedItem
{
get { return _gridSelectedItem; }
set
{
_gridSelectedItem = value;
OnPropertyChanged("GridSelectedItem");
}
}
public int SelectedGridIndex
{
get { return _selectedGridIndex; }
set
{
_selectedGridIndex = value;
OnPropertyChanged("SelectedGridIndex");
}
}
#endregion
public ICommand ChangeSelectedItemCommand
{
get { return _changeSelectedItemCommand ?? (_changeSelectedItemCommand = new SimpleCommand(p => ChangeSelectedGridItem())); }
}
private void ChangeSelectedGridItem()
{
SelectedGridIndex++;
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
演示对象类:
public class TestClass
{
public string Title { get; set; }
public string Name { get; set; }
}
和一些命令类:
public class SimpleCommand : ICommand
{
private readonly Predicate<object> _canExecuteDelegate;
private readonly Action<object> _executeDelegate;
#region Constructors
public SimpleCommand(Action<object> execute)
: this(execute, null)
{
}
public SimpleCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_executeDelegate = execute;
_canExecuteDelegate = canExecute;
}
#endregion // Constructors
#region ICommand Members
public virtual bool CanExecute(object parameter)
{
return _canExecuteDelegate == null || _canExecuteDelegate(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_executeDelegate(parameter);
}
#endregion
}
M AKE一定要添加在DataContext视图,使其知道您的视图模型:
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
我希望这可以帮助你获得预期的效果。
我想我错过了一些东西。这段代码似乎根本没有操纵焦点逻辑。我做了一个相当类似的方法。当我按下编辑SelectedItem属性的链接时,数据绑定会更新数据网格。所以我期望有可能通过数据绑定设置FocusedItem –
这个片段会影响所选项目的背景。您可以只更改背景颜色,并将注意力集中在单击的项目上,或者获取真实的DataGridCell或DataGridRow并将其传递给转换器,您可以在实际的GUI对象上设置.Focus()属性。这会将焦点从listview项目移动到DataGridRow/Cell –