I am new to WPF and have hit a wall trying to solve a seemingly straightforward problem.
I need to design a table of data and allow users to edit it. When user starts editing a cell I need to display a set of buttons in the rightmost column [OK] and [Cancel] to either accept or cancel the changes. When user is not editing a cell a [Delete] button should be displayed for user to delete the row.
I wrote a custom control that would display either [OK][Cancel] or a single [Delete] button based on the custom IsInEditMode property.
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
}
}
I need to somehow set IsInEditMode when user starts editing a cell. I've been looking all over msdn and this forum for an example/way how to do it, but can't find anything.
I add my custom control to the last column programmatically like this:
{
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}
}
);
}
Thank you very much for any info and tips!
I solved this problem using styles:
<Style x:Key="MwControlCellStyle" TargetType="{x:Type notesList:RowEditControl}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<!-- find DataGridRow parent object and trigger if it is in editing mode -->
<Condition Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=IsEditing}" Value="True"></Condition>
</MultiDataTrigger.Conditions>
<Setter Property="IsInEditMode" Value="True"></Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>