专注于xaml或ViewModel中的一行

时间:2011-02-17 10:35:38

标签: wpf mvvm datagrid

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

我有一个绑定为SelectedItem的属性。当在网格上更改选择并且单元格被聚焦时,绑定属性会更新,但是当我在视图模型中更改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中选择正确的项目。单击列表中的项目时,此项目将获得背景颜色。我试图以编程方式说明此行为。

由于

1 个答案:

答案 0 :(得分:0)

以下代码允许您在代码中设置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;
}