-1
<Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding Width1}*" />
            <ColumnDefinition Width="{Binding Width2}*" />
            <ColumnDefinition Width="{Binding Width3}*" />
</Grid.ColumnDefinitions>

I basically want whatever the value of Width1, Width2 ... to have an asterisk after it. So let's say the Width1 is 5, I want it to be 5* so that it will be a proportionate value.

  • 2
    It might help to clarify whether you want to make the grid's width proportional to another control higher up in the layout, or whether you just want to divide the grid's width into proportional columns. – estebro Jul 14 '19 at 17:15
  • @estebro I want the grid's width to be a proportional value rather than plain value. – Neil Nahid Jul 15 '19 at 02:45
  • Possible duplicate of [Setting ColumnDefinitions in code](https://stackoverflow.com/questions/5586563/setting-columndefinitions-in-code) – Peter Duniho Jul 15 '19 at 05:24
  • Possible duplicate of [Programmatically setting the width of a grid column with * in WPF](https://stackoverflow.com/questions/9803710/programmatically-setting-the-width-of-a-grid-column-with-in-wpf) – Peter Duniho Jul 15 '19 at 05:25
  • Now that you've clarified your question, it's clear it's just a duplicate of already-answered questions. See two of them referenced in my comments above. Instead of making your `Width1` etc. properties some numeric value (e.g. `double` or whatever it is you're using in your view model) make them a `GridLength` value, and set that to whatever you want. – Peter Duniho Jul 15 '19 at 05:26

1 Answers1

-1

I'm fairly new too WPF myself but I would do something like this:

enter image description here

And then you can do your binding on the WidthValString:

<Grid.ColumnDefinitions>
            <ColumnDefinition Width="{Binding WidthValString}" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

I'm assumming you will do this with ColumnDefinitions because you cannot have a grid with a width of 5*. As far as I know it has to be an integer. (With ColumnDef/RowDefs you'll be fine)

If this is not what you want, please specify your question!

Aleandro
  • 43
  • 7
  • 1
    I'm sorry, I've edited my question, and yes, what you assumed is exactly what I wanted to happen. I tried your solution and it doesn't work mainly because I think ColumnDefinition.Width doesn't accept strings? – Neil Nahid Jul 15 '19 at 02:46
  • Thats weird, works fine here. Do you get an error? Or it just won't work? – Aleandro Jul 15 '19 at 09:38
  • Well, this can't work at all, you'll get an `BindingException` because the `Width` property is a `GridLength` not a `string` nor an `int`. For more info see: https://learn.microsoft.com/en-US/dotnet/api/system.windows.controls.columndefinition?view=netframework-4.6 – k1ll3r8e Jul 21 '19 at 19:00