Donate

WPF MVVM - How To Pass ViewModel Or Element As ConverterParameter Value In XAML Code

Good evening!

In this post, I will demonstrate on how to pass a view model or an element as ConverterParameter value in XAML. This methodology is widely used by developers if you need to retrieve certain values or properties from a view model object or element. To send the view model data context as ConverterParameter value, reference the view model class in the data context node of your form or user control.
<UserControl d:DesignHeight="450" d:DesignWidth="800">

    <UserControl.DataContext>
        <localmain:ConfigurationResourceManagerViewModel x:Name="vmResourceManager" />
    </UserControl.DataContext>
	
</UserControl>
In your element as to where the ConverterParameter is, set it's value with the view model name. In the example snippet below, the ConverterParameter is used in the ComboBox SelectedItem property with a reference to the view model object.
<DataGridTemplateColumn Header="Resource Type" Width="120" IsReadOnly="False">
	<DataGridTemplateColumn.CellTemplate>
		<DataTemplate>
			<ComboBox ItemsSource="{Binding Path=DataContext.ResourcesTypes, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
					  SelectedItem="{Binding ResourcesType.ResourceTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ConfigurationResourceTypeConverter}, ConverterParameter={x:Reference Name=vmResourceManager}}"
					  DisplayMemberPath="ResourceTypeName">
			</ComboBox>
		</DataTemplate>
	</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
In your Converter class, cast the parameter variable with the view model class name and obtain the property or object that will be used in your Convert method logic.
public class ConfigurationResourceTypeConverter : IValueConverter
{
	  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	  {
		 int resourceTypeId;
		 stResourcesType item;
		 ObservableCollection<stResourcesType> stResourcesTypes;

		 resourceTypeId = (int)value;
		 stResourcesTypes = ((ConfigurationResourceManagerViewModel)parameter).ResourcesTypes;
		 item = stResourcesTypes.FirstOrDefault(x => x.ResourceTypeID == resourceTypeId);
		 
		 return item;
	  }

	  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	  {
		 stResourcesType item;

		 item = value as stResourcesType;
		 if (item == null)
			return 0;

		 return item.ResourceTypeID;
	  }
}
You can also pass a control or element to the ConverterParameter. In the example code below, I passed the DataGrid control referencing it's name.
<DataGridTemplateColumn Header="Resource Type" Width="120" IsReadOnly="False">
	<DataGridTemplateColumn.CellTemplate>
		<DataTemplate>
			<ComboBox ItemsSource="{Binding Path=DataContext.ResourcesTypes, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" 
					  SelectedItem="{Binding ResourcesType.ResourceTypeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ConfigurationResourceTypeConverter}, ConverterParameter={x:Reference Name=dataGridResources}}"
					  DisplayMemberPath="ResourceTypeName">
			</ComboBox>
		</DataTemplate>
	</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
In your Converter class, cast the parameter variable as DataGrid object and from then on, you can basically access all of the control's properties such as getting the ItemsSource value.
public class ConfigurationResourceTypeConverter : IValueConverter
{
	  public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
	  {
		 stResources item;
		 List<stResources> lstResources = ((DataGrid)parameter).ItemsSource as List<stResources>;
		 
		 //more codes here...
		 
		 return  item;
	  }

	  public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
	  {
		 stResourcesType item;

		 item = value as stResourcesType;
		 if (item == null)
			return 0;

		 return item.ResourceTypeID;
	  }
}
WPF MVVM - How To Pass ViewModel Or Element As ConverterParameter Value In XAML Code


Cheers!

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.