[英]Pressing Tab Key in last column of last row in DataGrid should set focus to first column of new row
[英]Datagrid not adding a new row by pressing Enter or Tab
我有一個數據網格,其中CanUserAddRows = "true"
。
當我運行程序時,我得到一個空白行。 當我在該行中輸入數據時,它運行良好。 但是之后,如果我嘗試添加新行,則無法添加它,因為按Enter或TAB不會出現空白行。
這是我的數據網格所在的頁面:
<Page x:Class="WPF_Client.Pages.Masters.Multiple.Groups"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:helpers="clr-namespace:WPF_Client.HelperClasses"
xmlns:vm="clr-namespace:WPF_Client.ViewModels.Masters.Multiple"
mc:Ignorable="d"
d:DesignHeight="760" d:DesignWidth="1366"
Title="Groups">
<Page.DataContext>
<vm:GroupsViewModel />
</Page.DataContext>
<Page.Resources>
<CollectionViewSource x:Key="GroupNamesWithCorrespondingEffectsCollection" Source="{Binding GroupNamesWithCorrespondingEffects}" />
</Page.Resources>
<Grid>
<DataGrid CanUserAddRows="True" CanUserReorderColumns="False" CanUserSortColumns="False" CanUserDeleteRows="True"
ItemsSource="{Binding Groups}" AutoGenerateColumns="False">
<DataGrid.Resources>
<CompositeCollection x:Key="Items">
<ComboBoxItem IsEnabled="False" Background="#FF2A2A2A" Foreground="White">
<Grid TextElement.FontWeight="Bold" >
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition Width="50" />
<ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>
<Grid.Children>
<TextBlock Grid.Column="0" Text="Group Name" />
<TextBlock Grid.Column="2" Text="Effect" />
</Grid.Children>
</Grid>
</ComboBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource GroupNamesWithCorrespondingEffectsCollection}}" />
</CompositeCollection>
<DataTemplate DataType="{x:Type helpers:GroupNameWithCorrespondingEffect}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition Width="50" />
<ColumnDefinition SharedSizeGroup="B" />
</Grid.ColumnDefinitions>
<Grid.Children>
<TextBlock Grid.Column="0" Text="{Binding GroupName}" />
<TextBlock Grid.Column="2" Text="{Binding CorrespondingEffect}" />
</Grid.Children>
</Grid>
</DataTemplate>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Name" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding GroupName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Group" Width="2*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{DynamicResource Items}"
SelectedValue="{Binding DataContext.SelectedGroupID, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}"
SelectedValuePath="GroupID" Grid.IsSharedSizeScope="True" TextSearch.TextPath="GroupName">
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Effect" Width="*" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataContext.Effects, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" DisplayMemberPath="Effect"
SelectedValue="{Binding DataContext.SelectedEffectID, RelativeSource={RelativeSource AncestorType={x:Type Page}}}" SelectedValuePath="EffectID"
Visibility="{Binding Path=DataContext.SelectedGroupID,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}},
Converter={StaticResource effectsVisibilityConverter}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Page>
這是我的ViewModel:
public class GroupsViewModel : ViewModelBase, IModule
{
public GroupsViewModel()
{
SessionViewModel.Instance.ModulesOpen.Add((IModule)this);
Groups = new ObservableCollection<Group>();
using (Entities db = new Entities())
{
List<GroupNameWithCorrespondingEffect> _GroupNamesWithCorrespondingEffects = (
from g in db.Groups
select new GroupNameWithCorrespondingEffect
{
GroupID = g.GroupID,
GroupName = g.GroupName,
CorrespondingEffect = g.Master_Effects.Effect
}
).ToList();
GroupNamesWithCorrespondingEffects
= new ObservableCollection<GroupNameWithCorrespondingEffect>(
_GroupNamesWithCorrespondingEffects.Where
(
u => !StaticMethods.GetAllChildren(25)
.Select(x => x.GroupID)
.Contains(u.GroupID)
).ToList()
);
Effects = new ObservableCollection<Master_Effects>(from m in db.Master_Effects
select m);
}
}
~GroupsViewModel()
{
SessionViewModel.Instance.ModulesOpen.Remove((IModule)this);
}
private ObservableCollection<Group> _groups;
public ObservableCollection<Group> Groups
{
get
{
return _groups;
}
set
{
_groups = value;
OnPropertyChanged("Groups");
}
}
private ObservableCollection<GroupNameWithCorrespondingEffect> _groupNamesWithCorrespondingEffects;
public ObservableCollection<GroupNameWithCorrespondingEffect> GroupNamesWithCorrespondingEffects
{
get
{
return _groupNamesWithCorrespondingEffects;
}
set
{
_groupNamesWithCorrespondingEffects = value;
OnPropertyChanged("GroupNamesWithCorrespondingEffects");
}
}
private int _selectedGroupID;
public int SelectedGroupID
{
get
{
return _selectedGroupID;
}
set
{
_selectedGroupID = value;
OnPropertyChanged("SelectedGroupID");
}
}
private ObservableCollection<Master_Effects> _effects;
public ObservableCollection<Master_Effects> Effects
{
get
{
return _effects;
}
set
{
_effects = value;
OnPropertyChanged("Effects");
}
}
private int _selectedEffectID;
public int SelectedEffectID
{
get
{
return _selectedEffectID;
}
set
{
_selectedEffectID = value;
OnPropertyChanged("SelectedEffectID");
}
}
public string ModuleFriendlyName
{
get { return "GroupsViewModel"; }
}
public string ModuleName
{
get { return "Groups"; }
}
}
我做錯什么了嗎? 還是dataGrid的默認行為?
更新:
我已經按照您的建議更新了代碼。 這是我的xaml:
<DataGrid ..............>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<i:InvokeCommandAction Command="{Binding DataContext.NewRowCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
這是我的ViewModel:
//在構造方法中:NewRowCommand = new RelayCommand(NewRow);
//在正文中:
public ICommand NewRowCommand { get; set; }
private void NewRow(object obj)
{
MessageBox.Show("PreviewKeyDown on Datagrid invoked.");
}
當我在DataGrid的輸入區域上按任意鍵時,我會成功在輸出中獲取MessageBox。
更新后的問題:
現在,在嘗試了您的建議點之后,我也遇到了一些問題。
我想僅在按DataGrid上的Enter鍵時顯示該消息框(如果這是網格上的默認行為。因為我是DataGrid的新手,所以我不知道默認行為。) 因此,我的實際問題是確定按下的鍵。
您必須將擊鍵事件“路由”到某些ViewModel方法,該方法會將新項添加到集合中。 在這種方法的最后,您必須調用OnPropertyChanged以在視圖中反映新項目。
編輯
<i:Interaction.Triggers> <i:EventTrigger> </i:EventTrigger> </i:Interaction.Triggers>
更多細節請看以下鏈接:
編輯2:
我看到了問題,在這種情況下,InputBindings會完成此工作:
<DataGrid.InputBindings>
<KeyBinding Key="Enter" Command="{Binding NewRowCommand }"/>
</DataGrid.InputBindings>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.