How to change the datagrid so that when it allows a placeholder new row to be available it has a hint in it as default. In this case <Enter New Record Here>. This uses FallbackValue.
Category Archives: MVVM
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(); } }
Changing a new datagrid row style
Changing a new datagrid placeholders row style:
WPF Commands
First Create a Delegate in ViewModel as a declaration:
public DelegateCommandDeploy { 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:
Adding a PRISMPROP snippet to VS C#
This sets up visual studio to add a snippet that will put in a prism style property really quickly and easily and help set this up fast!
FOLDER:
Documents\Visual Studio xxxx\Code Snippets\Visual C#\My Code Snippets\
FILE: prismprop.snippet
XML:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<CodeSnippetsĀ xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
<CodeSnippet Format=”1.0.0″>
<Header>
<Title>prismprop</Title>
<Shortcut>prismprop</Shortcut>
<Description>Code snippet for property and backing field</Description>
<Author>Microsoft Corporation</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property name</ToolTip>
<Default>Prop</Default>
</Literal>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>_myVar</Default>
</Literal>
</Declarations>
<Code Language=”csharp”><![CDATA[private $type$ $field$;
public $type$ $property$
{
get { return $field$;}
set { SetProperty<$type$>(ref $field$, value);}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>