gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:30:36 24 4
gpt4 key购买 nike

我有一个要求,我想在网格中的一行中添加一个新的空白行,它还需要处于编辑模式,在此模式下,我可以在逐个选项卡到达单元格后立即开始键入内容。

为此,我尝试使用数据网格的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项目模板完成的。请让我知道是否需要更多信息。

欢呼声

最佳答案

我遇到了同样的问题,我发现除了调用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

关于mvvm - Silverlight DataGrid.BeginEdit()不会将单元格置于编辑模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4473140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com