Skip to main content
Home Forums General Silverlight Getting Started XAML is the land of confusion ??
5 replies. Latest Post by mpalmer.sps on November 5, 2009.
(0)
mpalmer.sps
Member
1 points
12 Posts
11-03-2009 4:35 PM |
Still new to this and still not quite able to get a handle on how to navigate XAML for setting properties....
A great example:
The other day I had a ToggleButton.
Needed to set some content (Text) for the button.
Needed a hard coded LINEBREAK on the content in XAML.
Tried Several things
Tried ToggleButton.Content with Run tags split by a LineBreak - didn't work...Errors about Content being set "Multiple Times" ??
<ToggleButton HorizontalAlignment="Left" VerticalAlignment="Top"> <ToggleButton.Content> <Run>FOO</Run><LineBreak /><Run>Bar</Run> </ToggleButton.Content> </ToggleButton>
Then I tried adding a content control ... Same error about Content being set "Multiple Times"
<ToggleButton HorizontalAlignment="Left" VerticalAlignment="Top"> <ContentControl>FOO<LineBreak/></ContentControl>
</ToggleButton>
Finally I was able to get it to work with this code...
<ToggleButton HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid> <TextBlock>Foo<LineBreak/>Bar</TextBlock> </Grid> </ToggleButton>
Seriously????
Just to set the content on a button??
Is there a better way? Am I missing something or is this just a big pain in the butt?
[edit: Mispasted the examples]
swo
Contributor
2161 points
342 Posts
11-03-2009 4:55 PM |
Hi
You don't need a grid insight the TogglButton
<StackPanel> <ToggleButton HorizontalAlignment="Left" VerticalAlignment="Top" > <ToggleButton.Content> <TextBlock> First Line <LineBreak/> Second Line </TextBlock> </ToggleButton.Content> </ToggleButton> <ToggleButton HorizontalAlignment="Left" VerticalAlignment="Top" > <ContentControl> <TextBlock> First Line <LineBreak/> Second Line </TextBlock> </ContentControl> </ToggleButton> </StackPanel>
11-03-2009 5:01 PM |
Thanks for the reply!
I guess my problem is that I don't see "HOW" to figure out something like that... aside from trial and error
Don't misunderstand me - that works...
But the big question in my mind is "How does a person figure out that kind of basic answer".
It seems perfectly logical to me that you should be able to set the content with text (only) and not have to write it inside a textblock control.
You were able to figure that out, but did you get there by 'guessing' at first? Or was it some logical progression that brought you to that solution?
11-03-2009 5:29 PM |
Simple:
Thats the trick: put this FrameworkElement as content whichcan do what you want. The Blend and Vs 2008 code compleation can be helpful what is possible
For more info abaut ContentControl see http://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.content(VS.95).aspx
Jeremy L...
330 points
59 Posts
11-04-2009 8:14 AM |
I think the biggest way it can "come together" for you is when you start to look at it not as XML, but a declarative way of managing classes. Keep in mind every element of your XAML is going to instantiate a class or load a property.
With your example, you were instantiating a class that has an attached "Content" property. Content is not a collection, so it can only contain a single subclass. This is where it can get confusing.
When you simply insert text, XAML is smart enough to realize this belongs in a TextBlock class, so the class is instantiated for you an injected into the content. However, once you start mixing other tags, then you need to be more specific. If you think about the fact that you have to instantiate a class, then you can decide that TextBlock makes sense. Then you can understand that TextBlock has a "Text" property that understands collections of sub-properties such as runs and line breaks, and you can place them into that Text piece.
Again, there are lots of nuances and some learning curve, but I think the biggest ah-hah moment I had in helping unravel it all was realizing that every tag instantiates a class and every property must be able to somehow parse the provided attribute, whether it's text or another class, etc.
Event your binding tags do this ... for example, {Binding Name} really says "instance the Binding class and pass Name into the constructor" whereas {Binding Path=Name,Mode=TwoWay} says, "instance or new a Binding class, then set Path to Name and Mode to TwoWay"
Hope that helps!
11-05-2009 3:18 PM |
Thanks Wolfgang and Jeremy,
I think what I'm having trouble with is the syntax itself and the fact that C# is so much easier to debug.
A simple example is the Binding text that Jeremy brought to light "{Binding Path=Name,Mode=TwoWay}" If there is a typo, I've not yet found a good way to quickly locate them in XAML.
Secondly the actual contruction of the text string for the above example is problematic because there is no real Intellisense or type ahead for this ...
Am I missing a cool method or tool that can assist?
I know Blend can do some of the work for you, and perhaps that is the answer, but if you're like me and prefer Visual Studio then it can be a pain.