Silverlight DataGrid.BeginEdit() 不会将单元格置于编辑模式
我有一个要求,我想添加一个新的空白行,我已经在网格中填充了一行,并且它还需要处于编辑模式,我可以在通过选项卡到达单元格后立即开始输入。
为此,我尝试使用数据网格的 BeginEdit 功能,但它似乎根本不起作用。
这是我的代码:MainPage.xaml
<UserControl x:Class="DataGridTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="192"
Width="356"
DataContext="{Binding Main, Source={StaticResource Locator}}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="Purple"
Text="{Binding Welcome}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap" />
<sdk:DataGrid AutoGenerateColumns="True" Height="100" HorizontalAlignment="Left" Margin="12,51,0,0" Name="dgTest" VerticalAlignment="Top" Width="332" ItemsSource="{Binding DataGridItems,Mode=TwoWay}" />
</Grid>
MainPage.xaml.cs
using System.Windows.Controls;
using DataGridTest.ViewModel;
using GalaSoft.MvvmLight.Messaging;
namespace DataGridTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Messenger.Default.Register<bool>(this, MakeDataGridEditable);
}
public void MakeDataGridEditable(bool flag)
{
if (flag)
{
dgTest.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateSource();
MainViewModel dataContext = DataContext as MainViewModel;
dgTest.SelectedIndex = dataContext.DataGridItems.Count - 1;
dgTest.CurrentColumn = dgTest.Columns[0];
dgTest.UpdateLayout();
dgTest.Focus();
dgTest.BeginEdit();
}
}
}
}
MainViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
namespace DataGridTest.ViewModel
{
public class MainViewModel : ViewModelBase
{
DataGridItem dataGridItem;
private ObservableCollection<DataGridItem> dataGridItems;
public ObservableCollection<DataGridItem> DataGridItems
{
get
{
return dataGridItems;
}
set
{
dataGridItems = value;
RaisePropertyChanged("DataGridItems");
}
}
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
dataGridItems = new ObservableCollection<DataGridItem>();
dataGridItem = new DataGridItem();
dataGridItems.Add(dataGridItem);
dataGridItem.ChangesCommitted += new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
}
}
void dataGridItem_ChangesCommitted(object sender, EventArgs e)
{
dataGridItem.ChangesCommitted -= new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
CreateNewDataGridItem();
dataGridItem.ChangesCommitted += new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
Messenger.Default.Send<bool>(true);
}
private void CreateNewDataGridItem()
{
this.dataGridItem = new DataGridItem();
this.dataGridItems.Insert(dataGridItems.Count, dataGridItem);
}
}
public class DataGridItem : IEditableObject
{
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public void BeginEdit()
{
}
public void CancelEdit()
{
}
public event EventHandler<EventArgs> ChangesCommitted;
public void EndEdit()
{
if (null != ChangesCommitted)
{
EventArgs e = new EventArgs();
ChangesCommitted(this, new EventArgs());
}
}
}
}
此测试应用程序是使用 MVVM Lighttoolkit 项目模板完成的。如果您需要更多信息,请告诉我。
干杯---杰格
I have a requirement where I want to add a new blank row I have filled in a row in the grid and it also needs to be in edit mode where I can start typing straight away after reaching on the cell by tab.
For this I tried to use the BeginEdit function of the datagrid but it doesn't seem to work at all.
Here is my code: MainPage.xaml
<UserControl x:Class="DataGridTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Height="192"
Width="356"
DataContext="{Binding Main, Source={StaticResource Locator}}" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<TextBlock FontSize="36"
FontWeight="Bold"
Foreground="Purple"
Text="{Binding Welcome}"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextWrapping="Wrap" />
<sdk:DataGrid AutoGenerateColumns="True" Height="100" HorizontalAlignment="Left" Margin="12,51,0,0" Name="dgTest" VerticalAlignment="Top" Width="332" ItemsSource="{Binding DataGridItems,Mode=TwoWay}" />
</Grid>
MainPage.xaml.cs
using System.Windows.Controls;
using DataGridTest.ViewModel;
using GalaSoft.MvvmLight.Messaging;
namespace DataGridTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Messenger.Default.Register<bool>(this, MakeDataGridEditable);
}
public void MakeDataGridEditable(bool flag)
{
if (flag)
{
dgTest.GetBindingExpression(DataGrid.ItemsSourceProperty).UpdateSource();
MainViewModel dataContext = DataContext as MainViewModel;
dgTest.SelectedIndex = dataContext.DataGridItems.Count - 1;
dgTest.CurrentColumn = dgTest.Columns[0];
dgTest.UpdateLayout();
dgTest.Focus();
dgTest.BeginEdit();
}
}
}
}
MainViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
namespace DataGridTest.ViewModel
{
public class MainViewModel : ViewModelBase
{
DataGridItem dataGridItem;
private ObservableCollection<DataGridItem> dataGridItems;
public ObservableCollection<DataGridItem> DataGridItems
{
get
{
return dataGridItems;
}
set
{
dataGridItems = value;
RaisePropertyChanged("DataGridItems");
}
}
public MainViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
dataGridItems = new ObservableCollection<DataGridItem>();
dataGridItem = new DataGridItem();
dataGridItems.Add(dataGridItem);
dataGridItem.ChangesCommitted += new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
}
}
void dataGridItem_ChangesCommitted(object sender, EventArgs e)
{
dataGridItem.ChangesCommitted -= new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
CreateNewDataGridItem();
dataGridItem.ChangesCommitted += new EventHandler<EventArgs>(dataGridItem_ChangesCommitted);
Messenger.Default.Send<bool>(true);
}
private void CreateNewDataGridItem()
{
this.dataGridItem = new DataGridItem();
this.dataGridItems.Insert(dataGridItems.Count, dataGridItem);
}
}
public class DataGridItem : IEditableObject
{
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public void BeginEdit()
{
}
public void CancelEdit()
{
}
public event EventHandler<EventArgs> ChangesCommitted;
public void EndEdit()
{
if (null != ChangesCommitted)
{
EventArgs e = new EventArgs();
ChangesCommitted(this, new EventArgs());
}
}
}
}
This test app is done using MVVM Lighttoolkit project template. Please do let me know if you need any more info.
Cheers---Jag
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,我发现除了调用
BeginEdit()
之外,您还需要处理PreparingCellForEdit()
事件以将焦点设置在TextBox< /code>:
我在这里找到了这个解决方案: http://forums.silverlight.net/t/152064 .aspx
I struggled with this same issue, I found that in addition to calling
BeginEdit()
you need to handle thePreparingCellForEdit()
event to set focus on theTextBox
:I found this solution here: http://forums.silverlight.net/t/152064.aspx
这个解决方法为我解决了这个问题。它不是直接调用
BeginEdit()
,而是使用 Dispatcher 来调用它。这个答案在这里找到:
https://xceed.com/forums/topic /BeginEdit-function-just-focus-the-selected-cell/
This workaround fixed the issue for me. Instead of calling
BeginEdit()
directly, it uses the Dispatcher to invoke it.This answer was found here:
https://xceed.com/forums/topic/BeginEdit-function-just-focus-the-selected-cell/