Answered by:
How to use XAML to share some of the properties of multiple TextBlocks

Question
-
Is there a standard way in XAML to collect similarly derived properties with the same values from several controls into one place?
What is the best way to do it?
The code below demonstrates the functionality I am thinking of with WPF TextBlocks, although it doesn't do what I want. It ignores the Canvas.Left and Canvas.Top property values and concatenates each successive fragment of text (starting at Left,Top=0,0).
Canvas.Left,Top are what the designer used in <TextBlocks>—lines which I duplicated and reformatted into the below. I didn't see any x, y properties in class TextBlock itself. (Perhaps I simply should be using something other than Canvas.Left, Top for the TextBlocks' x,y?)
<local:DrawingWindow x:Class="MyApp.WelcomeScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyApp"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="500"
Background="Black"
Margin="1,1,1,1">
<TextBlock Foreground="Orange" HorizontalAlignment="Left" Height="25" VerticalAlignment="Top" Width="350" FontFamily="FreeSans" FontSize="14">
<TextBlock Canvas.Left="100" Canvas.Top="100" Text="Now is the time\nfor all good men"/>
<TextBlock Canvas.Left="120" Canvas.Top="150" Text="to come to"/>
<TextBlock Canvas.Left="140" Canvas.Top="200" Text="the aid of their country."/>
</TextBlock>
</local:DrawingWindow>-Mrutyunjaya
Friday, August 3, 2018 10:50 AM
Answers
-
Yes you can but like I said using a grid with columns and rows is so much better. Doing that will automatically handle things like when the user resizes the window.
Lloyd Sheen
- Marked as answer by MRUTYUNJAYA.M Friday, August 10, 2018 4:51 PM
Friday, August 10, 2018 4:46 PM
All replies
-
1. You don't show what local:DrawingWindow is. Without the XAML anything I say will be a guess.
2. If the TextBlocks are not within a Canvas using Canvas.Left/Right will have no effect.
3. Seems that you are trying a WinForms way of designing the UI. DON'T. Most likely what you want is to host the TextBlocks in a Grid. Then create Grid.RowDefinitions and Grid.ColumnDefinitions. You can then use Grid.Column= and Grid.Row= to set the position of the TestBlocks
Lloyd Sheen
- Proposed as answer by Zhanglong WuMicrosoft contingent staff Wednesday, August 8, 2018 8:53 AM
Friday, August 3, 2018 5:58 PM -
2. If the TextBlocks are not within a Canvas using Canvas.Left/Right will have no effect.
Can it not be done hosting within Canvas ? But TextBlocks should be within a Canvas.
-Mrutyunjaya
Friday, August 10, 2018 6:48 AM -
Yes you can but like I said using a grid with columns and rows is so much better. Doing that will automatically handle things like when the user resizes the window.
Lloyd Sheen
- Marked as answer by MRUTYUNJAYA.M Friday, August 10, 2018 4:51 PM
Friday, August 10, 2018 4:46 PM