Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multibinding in RowDefinition Height

Tags:

wpf

xaml

I want to make converter for row height, that is dependent on 3 variables. Two of them are from view model and one is constant string. I made MultiValueConverter for this, but obviously its not setting the RowDefinition.Height value.

The code looks like this:

<RowDefinition Name="Row1">
    <RowDefinition.Height>
        <MultiBinding Converter="{StaticResource MyConverter}">
            <Binding Path="PropertyFromViewModel1" />
            <Binding Source="{StaticResource DataGridName}" />
            <Binding Path="PropertyFromViewModel2" />
        </MultiBinding>
        </RowDefinition.Height>
    </RowDefinition>

Converter is working, it returns proper values (as strings).

Code for multivalue converter:

public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!((values[0]) is bool))
            throw new ArgumentException("First argument 'value' must be of type bool");
        if (values[1] == null)
            throw new ArgumentException("Secound argument must be diffrent then null");
        if (!((values[2]) is bool))
            throw new ArgumentException("Third argument 'value' must be of type bool");

        var showParkedTransactionDataGrid = (bool)values[0];
        var datagridName = values[1].ToString();
        var isCustomerDiscountShowed = (bool)values[2];
        if (showParkedTransactionDataGrid)
        {
            if (datagridName == "ProductListDataGrid")
            {
                return isCustomerDiscountShowed ? "306" : "336";
            }
            else if (datagridName == "ParkedTransactionDataGrid")
            {
                return "*";
            }
        }
        else
        {
            if (datagridName == "ProductListDataGrid")
            {
                return "*";
            }
            else if (datagridName == "ParkedTransactionDataGrid")
            {
                return "0";
            }
        }
        return "";
    }

I used before IValueConverter and it was working on RowDefinision Height property, but multibinding isnt.

like image 250
user746571 Avatar asked Nov 22 '25 07:11

user746571


1 Answers

The Height is of type System.Windows.GridLength

Make sure you return that from the converter.

EDIT

And, by the way, the converter is not very well designed! It is completely dependent on the naming of the controls. It will be very hard to find errors when renaming the controls.

You should consider another way of doing this.

like image 140
Emond Avatar answered Nov 24 '25 07:11

Emond



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!