我是WPF的新手,并试图解决看似简单的问题。在编辑中修改WPF DataGrid中的用户控件
我需要设计一个数据表并允许用户编辑它。当用户开始编辑单元格时,我需要在最右边的列中显示一组按钮[确定]和[取消]以接受或取消更改。当用户未编辑单元格时,应显示[删除]按钮供用户删除该行。
我写了一个自定义控件,可以根据自定义IsInEditMode属性显示[确定] [取消]或单个[删除]按钮。
public partial class RowEditControl : UserControl
{
public static DependencyProperty
IsInEditModeProperty = DependencyProperty.Register("IsInEditMode",
typeof(bool),
typeof(RowEditControl),
new FrameworkPropertyMetadata(OnEditModeChanged));
private static void OnEditModeChanged(DependencyObject aD, DependencyPropertyChangedEventArgs aE)
{
//depending on the value show [Delete] or [Ok][Cancel] buttons
}
}
当用户开始编辑单元格时,我需要以某种方式设置IsInEditMode。我一直在寻找所有的MSDN和这个论坛的例子/方式如何做到这一点,但找不到任何东西。
添加我的自定义控件的最后一列程序是这样的:
{
mwTagList.Columns[1].Width = new DataGridLength(1, DataGridLengthUnitType.Star);
var fRowEditTemplate = new FrameworkElementFactory(typeof (RowEditControl));
fRowEditTemplate.AddHandler(
RowEditControl.DeleteClickedEvent,
new RoutedEventHandler(OnDeleteRowBtn)
);
fRowEditTemplate.AddHandler(
RowEditControl.OkClickedEvent,
new RoutedEventHandler(OnRowEditOk));
fRowEditTemplate.AddHandler(
RowEditControl.CancelClickedEvent,
new RoutedEventHandler(OnRowEditCancel));
mwTagList.Columns.Add(
new DataGridTemplateColumn()
{
Header = "Delete Row",
CellTemplate = new DataTemplate() {VisualTree = fRowEditTemplate}
}
);
}
非常感谢你的任何信息和提示!
谢谢!我使用上面的样式解决了这个问题,但是这看起来也很酷。这也是如何使用转换器的一个很好的例子,我是WPF的新手,从来没有使用它们。 –