お世話になっております。
行いたいこと
Button内に2つの<TextBlock/>
を配置して、1つ目は左寄せ、2つ目は中央に表示させたいと思っております。
下記のようにXAMLを記述すれば、1つ以上のコントロールを指定できることは分かったのですが、それぞれの配置を指定する方法は分かりませんでした。
解決方法をご存知の方、よろしくお願いいたします。
XAML
1<Button> 2 <StackPanel Orientation="Horizontal"> 3 <TextBlock Text="Left" /> <!-- 左寄せにしたい --> 4 <TextBlock Text="Center"/> <!-- 中央にしたい --> 5 </StackPanel> 6</Button>
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。

回答3件
0
Button内に2つの<TextBlock/>を配置して、1つ目は左寄せ、2つ目は中央に表示させたい
xml
1<Button HorizontalContentAlignment="Stretch"> 2 <Grid> 3 <TextBlock Text="Left" /> 4 <TextBlock Text="Center" HorizontalAlignment="Center" /> 5 </Grid> 6</Button> 7 8<!-- or --> 9 10<Button HorizontalContentAlignment="Stretch"> 11 <Grid> 12 <TextBlock Text="Left" /> 13 <TextBlock Text="Center" TextAlignment="Center" /> 14 </Grid> 15</Button>
Control.HorizontalContentAlignment プロパティ (System.Windows.Controls) | Microsoft Learn
FrameworkElement.HorizontalAlignment プロパティ (System.Windows) | Microsoft Learn
TextBlock.TextAlignment プロパティ (System.Windows.Controls) | Microsoft Learn
xml
1<Window 2 x:Class="Q96032.MainWindow" 3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 5 Width="800" 6 Height="450" 7 ThemeMode="System"> 8 <StackPanel> 9 <!-- オリジナル --> 10 <Button> 11 <StackPanel Orientation="Horizontal"> 12 <TextBlock Text="Left" /> 13 <TextBlock Text="Center" /> 14 </StackPanel> 15 </Button> 16 17 18 <Button HorizontalContentAlignment="Stretch"> 19 <Grid> 20 <TextBlock Text="Left" /> 21 <TextBlock Text="Center" HorizontalAlignment="Center" /> 22 </Grid> 23 </Button> 24 25 <!-- or --> 26 27 <Button HorizontalContentAlignment="Stretch"> 28 <Grid> 29 <TextBlock Text="Left" /> 30 <TextBlock Text="Center" TextAlignment="Center" /> 31 </Grid> 32 </Button> 33 </StackPanel> 34</Window>
投稿2025/07/20 15:35
総合スコア10166
0
ベストアンサー
StackPanelは決定事項なのかどうか分かりませんが、
それらしきものができた(無理やりですが)
c#
1 <Button Margin="2,2,2,2"> 2 <Grid Width="300"> 3 <Grid.ColumnDefinitions> 4 <ColumnDefinition Width="*" /> 5 <ColumnDefinition Width="*" /> 6 <ColumnDefinition Width="*" /> 7 </Grid.ColumnDefinitions> 8 <TextBlock Grid.Column="0" Text="左寄せ" HorizontalAlignment="Left"/> 9 <TextBlock Grid.Column="1" Text="中央" HorizontalAlignment="Center"/> 10 </Grid> 11 </Button> 12
Grid の width ="300" はボタンの幅に合わせてください。
投稿2017/10/12 09:28

退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
0
ググッてみただけですが
TextBlock.TextAlignment プロパティ
があり、デフォルトでは、TextAlignment.Leftだそうです。
これをTextAlignment.Centerにすればよさそうですね
投稿2017/10/12 08:51
総合スコア179
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。