WFP dxg数据网格,重点自动选择第一行

时间:2018-12-05 14:20:50

标签: wpf datagrid devexpress

<helpers:CustomGrid Grid.Row="0"
                    Margin="0"
                    AutoGenerateColumns="None"
                    x:Name="DataGrid"
                    IsTabStop="False"
                    ItemsSource="{Binding Orders}"
                    CurrentItem="{Binding CurrentOrder}">

         <helpers:CustomGrid.View>
                    <dxg:TableView Name="TView"
                                   AllowResizing="True"
                                   NavigationStyle="Cell"
                                   ShowFixedTotalSummary="True" 
                                   AllowLeaveFocusOnTab="True" 
                                   HorizontalScrollbarVisibility="Auto" 
                                   VerticalScrollbarVisibility="Auto"/>
          </helpers:CustomGrid.View>

我找不到任何设置为自动获取行的属性,即使在我的customgrid中也尝试过:

public CustomGrid()
{
     ItemsSourceChanged += OnItemsSourceChanged;
}

private void OnItemsSourceChanged(object sender, ItemsSourceChangedEventArgs e)
{

}

Orders是一个ObservableCollection。

但是它不会成功,因为我正在向我的ObservableCollection添加范围,而不是创建一个新的(这就是我想要的方式)

任何建议如何做到这一点?

2 个答案:

答案 0 :(得分:0)

这完全取决于但...您可以注册到包含您的dataGrid的用户控件的Loaded事件,然后执行一个BeginInvoke()来设置视图模型上的CurrentOrder,然后再次发出另一个BeginInvoke()来确定焦点。

示例:

public CustomGrid()
{
    Loaded += CustomGrid_Loaded;
}

    void CustomGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        Dispatcher.BeginInvoke(new System.Action(() =>
       {

           var vm = DataContext as PageViewModel;
           vm.CurrentOrder = vm.Orders[0];

           //now set focus..
           dataGrid.Focus();   //sometimes you may need to issue another BeginInvoke() when you hijack the event model

答案 1 :(得分:0)

如果要在渲染GridControl时使焦点对准行,可以将DataControlBase.AllowInitiallyFocusedRow设置为true。

<helpers:CustomGrid Grid.Row="0"
                    Margin="0"
                    AutoGenerateColumns="None"
                    x:Name="DataGrid"
                    IsTabStop="False"
                    ItemsSource="{Binding Orders}"
                    CurrentItem="{Binding CurrentOrder}"
                    AllowInitiallyFocusedRow="True">

如果您想在GridControl得到关注时随时选择第一行,则可以使用自定义TableView实现(或行为)来实现。

public class CustomTableView : TableView
{
    protected override void OnGotFocus(RoutedEventArgs e)
    {
        base.OnGotFocus(e);

        if (FocusedRowHandle < 0)
            FocusedRowHandle = (DataControl as GridControl).GetRowHandleByListIndex(0);
    }
}
相关问题