This was reported by a customer issue on the doc repo. Filing it here so that we can track making this improvement in the product.
Customer said:
This doesn't seem possible and it's a fundamental oversight. We should be able to set them directly in XAML and with bindings. Once again, WPF was the pinnacle of XAML It has been downhill since :(
What they wanted to do:
<TextBlock.Margin>
<Thickness Left="10"
Top="10"
Right="10"
Bottom="{Binding Path='BottomMargin', Mode=OneWay" />
</TextBlock.Margin>
Source issue: https://github.com/MicrosoftDocs/winrt-api/issues/549
Love this. Couple of things we need to be careful about ...
The interesting thing about structs is that they're value types. So you can't modify Margin.Left
(it generates a compiler error in C#). Instead you read out the Margin
, modify the Thickness
, and write the whole thing back.
That's not something that Binding
can handle, which is part of why WPF doesn't support Binding
on struct fields. It is something that x:Bind
can support, but it will require special handling to teach it the copy/modify/write pattern. (Which gets even more fun with nested structs, which maybe aren't worth supporting.)
The other interesting thing about being a value type is if you put an x:Name on it in markup, what you get in code behind is a _copy_ of the struct. So if you x:Name the struct T1
there's one copy of the Thickness
struct in the TextBlock
, another in copy in this.T1
. WPF does the right thing here and doesn't let you x:Name a value type.
or make Thickness
a class
Most helpful comment
Love this. Couple of things we need to be careful about ...
The interesting thing about structs is that they're value types. So you can't modify
Margin.Left
(it generates a compiler error in C#). Instead you read out theMargin
, modify theThickness
, and write the whole thing back.That's not something that
Binding
can handle, which is part of why WPF doesn't supportBinding
on struct fields. It is something thatx:Bind
can support, but it will require special handling to teach it the copy/modify/write pattern. (Which gets even more fun with nested structs, which maybe aren't worth supporting.)The other interesting thing about being a value type is if you put an x:Name on it in markup, what you get in code behind is a _copy_ of the struct. So if you x:Name the struct
T1
there's one copy of theThickness
struct in theTextBlock
, another in copy inthis.T1
. WPF does the right thing here and doesn't let you x:Name a value type.