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

时间:2010-12-17 17:25:51

标签: mvvm datagrid silverlight-4.0 mvvm-light

我有一个要求,我想添加一个新的空白行,我已经在网格中连续填充了一行,它还需要处于编辑模式,我可以在到达单元格后立即开始输入。< / p>

为此,我尝试使用datagrid的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项目模板完成。如果您需要更多信息,请告诉我。

干杯---杰格

2 个答案:

答案 0 :(得分:2)

我在同样的问题上遇到了困难,我发现除了调用BeginEdit()之外,您还需要处理PreparingCellForEdit()事件以将注意力集中在TextBox上:

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

答案 1 :(得分:0)

此解决方法为我解决了这个问题。它不是直接调用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/