边框之后如何绑定文本?

时间:2019-07-03 09:45:53

标签: c# wpf xaml

我使用3列。在第一篇中,我放了一个简单的Border,没问题,但是在第三列中,我想在Border中有一个Text,然后(在右边)用Wrap开始我的Text可能性。要插入文本,这很容易,但是我想使用Binding使之动态。请参阅下面的代码。

<Grid Grid.Row="1">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="10"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Border Grid.Column="0" Background="Red" Height="15" Width="15" BorderBrush="Red" CornerRadius="100" Margin="5" Padding="5" VerticalAlignment="Top"/>
    <DockPanel Grid.Column="2" >
        <TextBlock TextWrapping="WrapWithOverflow"  Height="60" Width="450" LineHeight="20" LineStackingStrategy="BlockLineHeight" MaxWidth="{Binding ElementName=myWindowName, Path=ActualWidth}">
                <Border Background="Red" CornerRadius="10" Padding="10,0,10,0" Margin="0 -16 0 -5" Visibility="Visible" >
                    <TextBlock Text="Color and random text" Foreground="White" FontSize="12" FontWeight="Medium" />
                </Border>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
            incididunt ut labore et dolore magna aliqua. 
        </TextBlock>
    </DockPanel>
</Grid>

1 个答案:

答案 0 :(得分:1)

将动态文本放入Run元素中:

<TextBlock TextWrapping="WrapWithOverflow"  Height="60" Width="450" LineHeight="20" LineStackingStrategy="BlockLineHeight" 
        MaxWidth="{Binding ElementName=myWindowName, Path=ActualWidth}">
    <Border Background="Red" CornerRadius="10" Padding="10,0,10,0" Margin="0 -16 0 -5" Visibility="Visible" >
        <TextBlock Text="Color and random text" Foreground="White" FontSize="12" FontWeight="Medium" />
    </Border>
    <Run x:Name="run" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " />
</TextBlock>

如果为Run元素赋予x:Name,则可以从后面的代码中动态设置其Text属性。您还可以将其Text属性绑定到视图模型的source属性:

<Run Text="{Binding DynamicText, Mode=OneWay}" />