【问题标题】:Make Center Text Alignment Textblock in Grid layout wpf在网格布局wpf中制作中心文本对齐文本块
【发布时间】:2014-03-20 12:37:10
【问题描述】:

我有网格布局,里面有很多控件,尤其是我的网格内的Textblock。 但我只想在具有该控件的特定列中进行居中文本对齐。

您可以在此处的网格中查看我的代码..

  <Grid.ColumnDefinitions>
        <ColumnDefinition Width="30"/>
        <ColumnDefinition Width="100">
            <ColumnDefinition.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextAlignment" Value="Left" />
                </Style>
            </ColumnDefinition.Resources>
        </ColumnDefinition>

我想在我定义的第二列中定义该列中占据的所有文本框可以具有居中样式选项。有人可以帮忙吗?

【问题讨论】:

    标签: c# wpf grid wpf-controls


    【解决方案1】:

    假设您的 TextBlock 属于 Grid.Column 1

    <Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
        <Style.Triggers>
            <Trigger Property="Grid.Column" Value="1">
                <Setter Property="HorizontalAlignment" Value="Center"></Setter>
                <Setter Property="VerticalAlignment" Value="Center"></Setter>
            </Trigger>             
        </Style.Triggers>
    </Style>
    

    【讨论】:

    • 感谢 Sajeetharan.. 但是如果我的文本块在边框内,我该如何设置样式.. 像 &lt;Border Grid.Column="1" Grid.Row="1" &gt;&lt;TextBlock Text="Minyak tanah" /&gt;&lt;/Border&gt; 我该如何设置样式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2014-02-12
    • 2016-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多