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


<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)
{
}

订单是一个可观察的集合。

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

有什么建议吗?

这一切都取决于。。。您可以注册到包含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

如果您希望在渲染GridControl时有一个聚焦行,可以设置DataControlBase。将AllowInitialyFocusedRow设置为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实现(或Behavior(来实现。

public class CustomTableView : TableView
{
protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);
if (FocusedRowHandle < 0)
FocusedRowHandle = (DataControl as GridControl).GetRowHandleByListIndex(0);
}
}

最新更新