How to use a IValueConverter multiple times with different values in WPF?

MERUN KUMAR MAITY 531 Reputation points
2021-12-11T14:45:23.687+00:00

I have a WPF application where I need to show a rectangle at the top of the title bar. But the Rectangle should not cover the whole title bar. For this purpose I use a IValueConverter which gives me the exact output which I want. I do a data binding with the width of that Rectangle and use the converter there.

My problem is I need to use another Rectangle on that title bar but I except different value from that converter. Because this time Rectangle size is very small. Some people suggest me to use converter parameter. I use that but I don't get any value from it.

Here is my IValueConverter in C# :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Globalization;

namespace WpfApp1
{
   public class WpConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return double.Parse(value.ToString()) - 142;
        }

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

Here is the Xaml code of my Rectangle:

 <Rectangle Fill="Yellow" Stroke="{x:Null}" Grid.Row="0" HorizontalAlignment="Stretch" Width="{Binding Path=ActualWidth, ElementName=xp, Converter={StaticResource WpConverter}}"  VerticalAlignment="Top" Height="1"  Margin="-140,0,0,0"  x:Name="top" PreviewMouseDown="Resize" MouseMove="DisplayResizeCursor" />

I need to use another similar Rectangle But If I use the same converter I get the same values. The main goal is to change this line return double.Parse(value.ToString()) - 142;

the another way I found is to declare another IValueConverter with different values. But that pretty awkward. I need to do something using converter parameter.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,710 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

Accepted answer
  1. Peter Fleischer (former MVP) 19,316 Reputation points
    2021-12-11T20:10:58.87+00:00

    HU,
    you can use ConverterParameter:

    <Rectangle Width="{Binding Path=ActualWidth, ElementName=xp, Converter={StaticResource WpConverter}, ConverterParameter=142}"  ...
    

    and converter:

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      double disp;
      if (parameter == null || !double.TryParse(parameter.ToString(), out disp)) disp =142;
      return double.Parse(value.ToString()) - disp;
    }
    
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }
    

0 additional answers

Sort by: Most helpful