gpt4 book ai didi

wpf - 关注 xaml 或 ViewModel 中的一行

转载 作者:行者123 更新时间:2023-12-03 10:50:28 24 4
gpt4 key购买 nike

我正在尝试从 XAML 或 ViewModel 将单元格焦点设置在 DataGrid 上。

我有一个绑定(bind)为 SelectedItem 的属性。当在网格上更改选择并且单元格被聚焦时,绑定(bind)属性会更新,但是当我在我的 View 模型中更改 SelectedItem 时,该行将被聚焦而不是单元格。

更改 SelectedItem 时,如何使其聚焦单元格?

这是 XAML

<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;
}

关于wpf - 关注 xaml 或 ViewModel 中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5027775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com