Category Archives: XAML

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.

Binding a TabItem visibility in XAML

This is a quick demo of how to bind your TabItem visibility so that you can quickly change what’s visible using variable in your viewmodel code.

Create a Converter in a new class fileĀ  Converters:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows;

namespace ConfigurationModule.Converters
{
    [ValueConversion(typeof(bool), typeof(Visibility))]
    public class BoolToVisibilityConverter : IValueConverter
    {

        #region IValueConverter Members
    
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is bool && targetType == typeof(Visibility))
            {
                bool val = (bool)value;
                if (val)
                    return Visibility.Visible;
                else
                    if (parameter != null && parameter is Visibility)
                        return parameter;
                    else
                        return Visibility.Collapsed;
            }
            throw new ArgumentException("Invalid argument/return type. Expected argument: bool and return type: Visibility");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Visibility && targetType == typeof(bool))
            {
                Visibility val = (Visibility)value;
                if (val == Visibility.Visible)
                    return true;
                else
                    return false;
            }
            throw new ArgumentException("Invalid argument/return type. Expected argument: Visibility and return type: bool");
        }
        #endregion
    }
}

 
In the XAML header page add:

xmlns:converters="clr-namespace:.Converters"

 
In the XAML Tabcontrol:


 


XAML relative size

How to size components to fit relative to parents in the tree:

Width=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}},Path=ActualWidth}”

Height=”{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}},Path=ActualHeight}”