Silverlight DataGrid.BeginEdit() 不会将单元格置于编辑模式

发布于 2024-10-08 07:25:55 字数 5101 浏览 1 评论 0原文

我有一个要求,我想添加一个新的空白行,我已经在网格中填充了一行,并且它还需要处于编辑模式,我可以在通过选项卡到达单元格后立即开始输入。

为此,我尝试使用数据网格的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-10-15 07:25:55

我遇到了同样的问题,我发现除了调用 BeginEdit() 之外,您还需要处理 PreparingCellForEdit() 事件以将焦点设置在 TextBox< /code>:

private void dgTest_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    TextBox tb = e.EditingElement as TextBox;
    if (tb != null)
    {
        tb.Focus();
    }
}

我在这里找到了这个解决方案: 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 the PreparingCellForEdit() event to set focus on the TextBox:

private void dgTest_PreparingCellForEdit(object sender, DataGridPreparingCellForEditEventArgs e)
{
    TextBox tb = e.EditingElement as TextBox;
    if (tb != null)
    {
        tb.Focus();
    }
}

I found this solution here: http://forums.silverlight.net/t/152064.aspx

谎言月老 2024-10-15 07:25:55

这个解决方法为我解决了这个问题。它不是直接调用 BeginEdit(),而是使用 Dispatcher 来调用它。

var action = new Action(() =>
{
    dataGrid.BeginEdit();
});
this.Dispatcher.BeginInvoke(action, System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);

这个答案在这里找到:
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.

var action = new Action(() =>
{
    dataGrid.BeginEdit();
});
this.Dispatcher.BeginInvoke(action, System.Windows.Threading.DispatcherPriority.ApplicationIdle, null);

This answer was found here:
https://xceed.com/forums/topic/BeginEdit-function-just-focus-the-selected-cell/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文