Wpf 关注xaml或ViewModel中的一行
我试图从XAML或ViewModel将单元格焦点设置在数据网格上 我有一个绑定为SelectedItem的属性。当在网格上更改选择并聚焦单元格时,绑定属性将更新,但是当我在视图模型中更改SelectedItem时,将聚焦行而不是单元格 更改SelectedItem时,如何使其聚焦单元格 这是XAMLWpf 关注xaml或ViewModel中的一行,wpf,mvvm,datagrid,Wpf,Mvvm,Datagrid,我试图从XAML或ViewModel将单元格焦点设置在数据网格上 我有一个绑定为SelectedItem的属性。当在网格上更改选择并聚焦单元格时,绑定属性将更新,但是当我在视图模型中更改SelectedItem时,将聚焦行而不是单元格 更改SelectedItem时,如何使其聚焦单元格 这是XAML <DataGrid SelectedItem="{Binding SelectedHotel}" ItemsSource="{Binding HotelsList}" AutoGenerate
<DataGrid SelectedItem="{Binding SelectedHotel}" ItemsSource="{Binding HotelsList}" AutoGenerateColumns="False" CanUserReorderColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="350" Header="Hotel" d:IsLocked="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding Name}"></Label>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<TextBox Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Content="Save" Command="{Binding DataContext.SaveEditsCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
<Button Grid.Column="1" Content="Delete" Command="{Binding DataContext.DeleteCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
我只需要在DataGrid中选择正确的项。当您单击列表中的某个项目时,该项目将获得背景色。我试图通过编程来说明这种行为
感谢以下代码允许您在代码中设置SelectedItem,并且当用户在网格中选择一行时,它也会更新。这就是你想要的吗?如果不是,你能扩大你的问题吗
public partial class MainWindow : Window, INotifyPropertyChanged
{
private ObservableCollection<Hotel> _hotelsList;
private Hotel _selectedHotel;
public ObservableCollection<Hotel> HotelsList
{
get { return _hotelsList; }
set
{
if (_hotelsList == value)
return;
_hotelsList = value;
OnPropertyChanged("HotelsList");
}
}
public Hotel SelectedHotel
{
get { return _selectedHotel; }
set
{
if (_selectedHotel == value)
return;
_selectedHotel = value;
OnPropertyChanged("SelectedHotel");
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
var hotels = new ObservableCollection<Hotel>();
hotels.Add(new Hotel { Name = "Royal Bath" });
hotels.Add(new Hotel { Name = "Royal Norfolk" });
hotels.Add(new Hotel { Name = "Hilton" });
HotelsList = hotels;
Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
SelectedHotel = HotelsList[1];
DataGridCell cell = GetCell(myGrid, 1, 0);
cell.Focus();
}
public DataGridCell GetCell(DataGrid grid, int row, int column)
{
DataGridRow rowContainer = GetRow(grid, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
grid.ScrollIntoView(rowContainer, grid.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(DataGrid grid, int index)
{
DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
grid.ScrollIntoView(grid.Items[index]);
row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual) VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Hotel : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value)
return;
_name = value;
OnPropertyChanged("Name");
}
}
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public分部类主窗口:窗口,INotifyPropertyChanged
{
私人可观察收集酒店列表;
私人酒店(精选酒店),;
公共可观察收集酒店列表
{
获取{return\u hotelsList;}
设置
{
如果(_hotelsList==值)
回来
_hotelsList=值;
OnPropertyChanged(“酒店列表”);
}
}
公共精选酒店
{
获取{return\u selectedHotel;}
设置
{
如果(_selectedHotel==值)
回来
_selectedHotel=价值;
OnProperty变更(“选定酒店”);
}
}
公共主窗口()
{
初始化组件();
DataContext=this;
var hotels=新的ObservableCollection();
添加(新酒店{Name=“Royal Bath”});
添加(新酒店{Name=“Royal Norfolk”});
添加(新酒店{Name=“希尔顿”});
HotelsList=酒店;
已加载+=已加载;
}
已加载专用void(对象发送方,RoutedEventArgs e)
{
SelectedHotel=HotelsList[1];
DataGridCell=GetCell(myGrid,1,0);
cell.Focus();
}
公共DataGridCell GetCell(DataGrid网格,int行,int列)
{
DataGridRow rowContainer=GetRow(网格,行);
if(rowContainer!=null)
{
DataGridCellsPresenter=GetVisualChild(行容器);
//尝试获取该单元,但它可能已虚拟化
DataGridCell=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
if(单元格==null)
{
//现在试着把手机放在视野里,然后拿回手机
ScrollIntoView(rowContainer,grid.Columns[column]);
单元格=(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(列);
}
返回单元;
}
返回null;
}
公共DataGridRow GetRow(DataGrid网格,int索引)
{
DataGridRow行=(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(索引);
if(行==null)
{
//可能已虚拟化,请查看并重试
grid.ScrollIntoView(grid.Items[index]);
行=(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(索引);
}
返回行;
}
公共静态T GetVisualChild(可视父级),其中T:VisualChild
{
T child=默认值(T);
int numVisuals=VisualTreeHelper.GetChildrenCount(父级);
对于(int i=0;i
我已经展开了我的问题。你的答案和我已经做的是一样的,但是它没有像我所希望的那样在UI中运行