Category Archives: WPF

Datagrid row delete button with new row placeholder disable

Aim:

To create a button on a datagrid to delete a row but not crash on the new row placeholder row.

Method:

Add a column like any other inside the datagrid but use a template type to show the button.

                        
                            
                                
                            
                        
                    

Add a converter to check for the new row place holder:

    [ValueConversion(typeof(string), typeof(Boolean))]
    public class PlaceHolderConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() == "{DataGrid.NewItemPlaceholder}")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

WPF Commands

First Create a Delegate in ViewModel as a declaration:

public DelegateCommand Deploy { get; set; } 

Next create the method you are going to call:

//Deploy Module Clicked - This one takes ro_clientmodules_extend from my grid!
private void Deploy_click(ro_clientmodules_extend obj)
{

}

Next sort out the viewmodel constructor:

public LicenseDeploymentModuleViewModel()
{
   Deploy = new DelegateCommand(Deploy_click);
}

Finally deal with the XAML:

Note: Mine is inside the Datagrid as a button on a row. Yours may need adjusting to tweek the relative path!
Note2: Mine is also bound to an enable or disable variable.


If you remove the references to ro_clientmodules_extend then you don’t need the complex additions to the binding although you will not then pass the variable back to the command procedure which could limit what you can do.. hence I left it in as an example.

WPF MessageBox

To create a WPF MessageBox use:

MessageBoxResult dialogResult = System.Windows.MessageBox.Show("Are you sure you wish to delete this?", "Delete Confirmation", System.Windows.MessageBoxButton.YesNo);
if (dialogResult == MessageBoxResult.Yes)
{
    System.Windows.MessageBox.Show("Delete operation Worked");
}
else
{
    System.Windows.MessageBox.Show("Delete operation Terminated");
}