C
C#17mo ago
Emelie

✅ Help understanding WPF (XAML)

Hi, I am having difficulty understand this snippet of code. I understand conceptually what it does - it adds a controltemplate to the specified targetType (in this case a button element). It does this by creating a grid instance and then an Ellipse object. The ellipse object and its attribute is "linked" to the button element. If I understand it correctly, that is. I think what mostly confuses me is why so many tags are used. Why do we need the <Setter Property="Template"> AND the <Setter.Value> ? why can´t we just add the controltemplate tag directly? because as far as I can see, the controltemplate already contains information about the property, value and targettype.
No description
5 Replies
Pobiega
Pobiega17mo ago
Because thats just how xaml syntax works Setter.Value is the property Value from the setter class, but used as an element because we want to put xaml in that property
JakenVeina
JakenVeina17mo ago
Setter is a class, and the sytanx in XAML for instantiating objects is <Class /> or <Class></Class> the syntax in XAML for setting properties of an object is <Class Property="Value"/> if the value is a string or has string-compatible markup type converter, or <Class><Class.Property>Value</Class.Property></Class> setting up your style involves creating an instance of Setter, and setting its Value property to an instance of ControlTemplate, ergo that is the correct syntax for doing those thins in XAML
Klarth
Klarth17mo ago
Avalonia can do this implicitly without <Setter.Value> because of the [Content] attribute which is nice... https://github.com/AvaloniaUI/Avalonia/blob/master/src/Avalonia.Base/Styling/Setter.cs#L48
JakenVeina
JakenVeina17mo ago
well, WPF has that too I wasn't sure if Setter.Value has that annotation I'd assumed not, since I've never seen it omitted
Emelie
EmelieOP17mo ago
Thank you all! I understand it now

Did you find this page helpful?