Last active
August 29, 2015 14:09
-
-
Save BryanWilhite/c0bf02d16a1a0794055d to your computer and use it in GitHub Desktop.
C#: Silverlight/Telerik: RadGridViewEditCommandsColumn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows; | |
using My.Client.Views.Administration; | |
using Telerik.Windows.Controls; | |
using Telerik.Windows.Controls.GridView; | |
namespace My.Client.Views | |
{ | |
using My.Silverlight.Extensions; | |
public class RadGridViewEditCommandsColumn : GridViewBoundColumnBase | |
{ | |
/// <summary> | |
/// Gets or sets the action after delete. | |
/// </summary> | |
/// <value> | |
/// The action after delete. | |
/// </value> | |
public Action<object> ActionAfterDelete { get; set; } | |
#region EditCommandWindow: | |
public DefaultDataFormWindow EditCommandWindow | |
{ | |
get { return (DefaultDataFormWindow)GetValue(EditCommandWindowProperty); } | |
set { SetValue(EditCommandWindowProperty, value); } | |
} | |
public static readonly DependencyProperty EditCommandWindowProperty = | |
DependencyProperty.RegisterAttached("EditCommandWindow", | |
typeof(DefaultDataFormWindow), | |
typeof(RadGridViewEditCommandsColumn), | |
new PropertyMetadata(null)); | |
#endregion | |
#region IsEditButtonVisible: | |
/// <summary> | |
/// Gets or sets a value indicating whether this instance is edit button visible. | |
/// </summary> | |
/// <value> | |
/// <c>true</c> if this instance is edit button visible; otherwise, <c>false</c>. | |
/// </value> | |
public bool IsEditButtonVisible | |
{ | |
get { return (bool)GetValue(IsEditButtonVisibleProperty); } | |
set { SetValue(IsEditButtonVisibleProperty, value); } | |
} | |
public static readonly DependencyProperty IsEditButtonVisibleProperty = | |
DependencyProperty.Register("IsEditButtonVisible", | |
typeof(bool), | |
typeof(RadGridViewEditCommandsColumn), | |
new PropertyMetadata(true, | |
(s, args) => | |
{ | |
var that = s as RadGridViewEditCommandsColumn; | |
if (that == null) throw new Exception("The expected source is not here."); | |
var newValue = (bool)args.NewValue; | |
that.SetIsEditButtonVisible(newValue); | |
})); | |
void SetIsEditButtonVisible(bool newValue) | |
{ | |
if (this._control == null) return; | |
if (newValue) | |
{ | |
this._control.EditButton.Visibility = Visibility.Visible; | |
} | |
else | |
{ | |
this._control.EditButton.Visibility = Visibility.Collapsed; | |
} | |
} | |
#endregion | |
public override bool CanEdit(object item) | |
{ | |
return false; | |
} | |
public override FrameworkElement CreateCellElement(GridViewCell cell, object dataItem) | |
{ | |
this._grid = this.DataControl as RadGridView; | |
if (this._grid == null) return null; | |
this._control = new RadGridViewEditCommandsControl(); | |
this._control.DeleteButton.Click += (s, args) => | |
{ | |
if (this._grid.RowInEditMode != null) return; | |
cell.ParentRow.IsSelected = true; | |
RadWindow.Confirm("Are you sure you want to delete this item?", | |
(o, e) => | |
{ | |
if (!e.DialogResult.GetValueOrDefault()) return; | |
this._grid.ExecuteDeleteCommand(this.ActionAfterDelete, cell.ParentRow); | |
}); | |
}; | |
this._control.EditButton.Click += (s, args) => | |
{ | |
if (this._grid.RowInEditMode != null) return; | |
cell.ParentRow.IsSelected = true; | |
var window = this.EditCommandWindow; | |
if (window == null) return; | |
window.ParentGridView = this._grid; | |
window.DataForm.CurrentItem = this._grid.CurrentItem; | |
window.ShowDialog(); | |
}; | |
if (!this.IsEditButtonVisible) | |
this._control.EditButton.Visibility = Visibility.Collapsed; | |
return this._control; | |
} | |
RadGridView _grid; | |
RadGridViewEditCommandsControl _control; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment