【问题标题】:wpf datagrid focus on the selectemItemwpf datagrid 专注于 selectemItem
【发布时间】:2013-02-21 19:16:32
【问题描述】:

我以编程方式在数据网格中选择项目。 问题是我必须手动向下滚动到 selectItem。我需要自动执行此操作。 到目前为止,我已经尝试了很多东西,但对我没有任何作用......

数据网格:

        <DataGrid x:Name="coreServiceLogDataGrid"
              ItemsSource="{Binding}"
              IsReadOnly="True"
              RowDetailsVisibilityMode="VisibleWhenSelected"
              SelectionMode="Single"
              IsSynchronizedWithCurrentItem="True"
              SelectedItem="{Binding Path=CurrentCoreServiceLogDataItem,Source={StaticResource synchronizer}, Mode=TwoWay}"
              GotFocus="coreServiceLogDataGrid_GotFocus_1"
              Style="{DynamicResource ResourceKey=dataGridStyle}"
              ...>
              ...
    </DataGrid>

以及GotFocus 的代码:

        private void coreServiceLogDataGrid_GotFocus_1(object sender, System.Windows.RoutedEventArgs e) {
          if (coreServiceLogDataGrid.SelectedItem != null) {
            coreServiceLogDataGrid.ScrollIntoView(coreServiceLogDataGrid.SelectedItem);
          }
         }

【问题讨论】:

  • 您是否验证了事件GotFocus 实际触发并且coreServiceLogDataGrid.SelectedItem 不为空?
  • @AbZy 我已经更新了代码,但它仍然对我不起作用。

标签: c# wpf datagrid focus


【解决方案1】:

我过去也遇到过类似的问题。我已经为DataGridRow 完成了此操作,但它基于在此site 上找到的TreeViewItem 的附加行为。

这是code-behind的代码:

/// <summary>
/// Exposes attached behaviors that can be
/// applied to DataGridRow objects.
/// </summary>
public static class DataGridRowBehavior
{
    #region IsBroughtIntoViewWhenSelected

    public static bool GetIsBroughtIntoViewWhenSelected(DataGridRow dataGridRow)
    {
        return (bool)dataGridRow.GetValue(IsBroughtIntoViewWhenSelectedProperty);
    }

    public static void SetIsBroughtIntoViewWhenSelected(
      DataGridRow dataGridRow, bool value)
    {
        dataGridRow.SetValue(IsBroughtIntoViewWhenSelectedProperty, value);
    }

    public static readonly DependencyProperty IsBroughtIntoViewWhenSelectedProperty =
        DependencyProperty.RegisterAttached(
        "IsBroughtIntoViewWhenSelected",
        typeof(bool),
        typeof(DataGridRowBehavior),
        new UIPropertyMetadata(false, OnIsBroughtIntoViewWhenSelectedChanged));

    static void OnIsBroughtIntoViewWhenSelectedChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        DataGridRow item = depObj as DataGridRow;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
            item.Selected += OnDataGridRowSelected;
        else
            item.Selected -= OnDataGridRowSelected;
    }

    static void OnDataGridRowSelected(object sender, RoutedEventArgs e)
    {
        // Only react to the Selected event raised by the TreeViewItem
        // whose IsSelected property was modified. Ignore all ancestors
        // who are merely reporting that a descendant's Selected fired.
        if (!Object.ReferenceEquals(sender, e.OriginalSource))
            return;

        DataGridRow item = e.OriginalSource as DataGridRow;
        if (item != null)
            item.BringIntoView();
    }

    #endregion // IsBroughtIntoViewWhenSelected
}

在您的XAML 中,将此代码放在DataGrid 的标签之间:

<DataGrid.ItemContainerStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="uc:DataGridRowBehavior.IsBroughtIntoViewWhenSelected" Value="True" />
    </Style>
</DataGrid.ItemContainerStyle>
/// note: uc is a namespace I have defined for where the DataGridRowBehavior class is located

附加评论:我将SelectionUnit 设置为FullRowSelectionMode 设置为Single。我不确定更改这些属性是否会影响这是否有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多