XAML 属性元素中不能同时包含文本和子元素“LineBreak”

7vux5j2d  于 2023-08-01  发布在  其他
关注(0)|答案(2)|浏览(103)

我有以下XAML:

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Margin="15">
        line1<LineBreak/><LineBreak/>
        line2
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding State}" Value="ShowText">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>

字符串
但是Visual Studio会抛出错误
第一个月
我不知道如何保持断线,也解决了这个问题。我试过这个:

<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap" Margin="15">
        <TextBlock.Text>
        line1<LineBreak/><LineBreak/>
        line2
        </TextBlock.Text>
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding State}" Value="ShowText">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>


但随后得到重复错误“文本属性设置多次”。

qgelzfjb

qgelzfjb1#

将内联放在样式后面:

<TextBlock ...>
    <TextBlock.Style>
        ...
    </TextBlock.Style>
    line1<LineBreak/><LineBreak/>
    line2
</TextBlock>

字符串

lztngnrs

lztngnrs2#

或使用<Run>换行

<TextBlock ...>
    <Run>line1</Run>
    <LineBreak/><LineBreak/>
    <Run>line2</Run>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            ...
        </Style>
    </TextBlock.Style>
</TextBlock>

字符串

相关问题