Search code examples
c#wpfdata-bindingwidth

Wpf Width binding to property with star


Can you bind the width of a property to the value of an object times star, for example,

(Width="{Binding SizeInt*}") in xaml?

In the code behind you can do this:

rect.Width = new GridLength(item.SizeInt, GridUnitType.Star);

I want to be able to have the same effect in xaml like so...

<Rectangle x:Name="rect" Fill="Red" Width="{Binding SizeInt*}"/>

Is there a way to do this using xaml?


Solution

  • Try to bind to a GridLength souce property:

    public GridLength SizeInt => new GridLength(1, GridUnitType.Star);
    

    You can replace 1 with any value you want, including the value of another field.