WPF DataGrid将行选择更改为单元格的TextBox

时间:2014-07-14 20:33:30

标签: c# wpf datagrid textbox

我有一个DataGrid,当用户点击向下箭头时,它需要移动到下面的下一行,但是要关注一个有文本框的单元格。我不想专注于文本框而不是实际的单元格。这就是我的DataGrid的样子;

<DataGrid Name="myDataGrid" PreviewKeyDown="myDataGrid_PreviewKeyDown">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                    <TextBlock HorizontalAlignment="Right" Text="{Binding Path=Descrip, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, StringFormat=C, ConverterCulture=en-us}" TextAlignment="Right"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Units Counted" Width="Auto">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox BorderBrush="Black" BorderThickness="2" FontWeight="Bold" Margin="2" Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

private void myDataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Down)
        {
            e.Handled = true;
            int indx = myDataGrid.SelectedIndex + 1;
            myDataGrid.SelectedIndex = indx;
            myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[1])
        }
    }

执行此操作选择单元格而不是文本框。如果我将其添加到文本框;

FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"

它将光标添加到每个行文本框但不关注文本框。

感谢

编辑:感谢@Sheridan,我更接近这段代码;

<TextBox 
BorderBrush="Black" 
BorderThickness="2" 
FontWeight="Bold" 
Margin="2" 
Text="{Binding Path=UnitCounted, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
TextAlignment="Right" 
FocusManager.IsFocusScope="True"
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"/>

然后在myDataGrid_PreviewKeyDown添加myDataGrid.BeginEdit()允许它工作。问题是看起来BeginEdit实际上从未真正结束。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我不会惹恼FocusManager.FocusedElement,因为你会得到一些未知的警告,它可以像你现在使用BeginEdit那样做。

在xaml

中,我所关注的TextBox很简单
<DataGrid Name="myDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Loans}" PreviewKeyDown="UIElement_OnPreviewMouseDown">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Units Counted" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox x:Name="sampleTextBox" HorizontalAlignment="Right" Text="{Binding Name}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

在课程档案中​​我有

    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 UIElement_OnPreviewMouseDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Down) return;
        e.Handled = true;
        var indx = myDataGrid.SelectedIndex + 1;
        myDataGrid.SelectedIndex = indx;
        myDataGrid.CurrentCell = new DataGridCellInfo(myDataGrid.Items[indx], myDataGrid.Columns[0]);

        var row = this.myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item) as DataGridRow;
        var presenter = GetVisualChild<DataGridCellsPresenter>(row);


        var cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromItem(myDataGrid.CurrentCell.Item);
        if (cell != null)
        {
            var contentPresenter = cell.Content as ContentPresenter;
            if (contentPresenter != null)
            {
                var m = contentPresenter.ContentTemplate.FindName("sampleTextBox", contentPresenter);
                ((TextBox) m).Focus();
            }
        }
    }
}

现在,每次按下TextBox键时,Down都会成为焦点,并更改单元格选择。