Skip to main content
 首页 » 编程设计

c#之XAML 标签 : binding text content when using text styles

2024年02月23日18kevingrace

我有以下 XAML:

<Label> 
    <Underline Foreground="Blue">Foo</Underline> 
</Label> 

现在我想在运行时使用绑定(bind)替换文本“Foo”,但显然我不能在这里放置 {..} 绑定(bind)来代替 Foo。执行此操作的正确方法是什么?

请您参考如下方法:

我遇到了这个,

<Label> 
    <Underline Foreground="Blue"> 
        <Underline.Inlines> 
            <TextBlock Text="{Binding Text}"></TextBlock> 
        </Underline.Inlines> 
    </Underline> 
</Label> 

事实上,您可以将其简化为

<Label> 
    <Underline Foreground="Blue"> 
        <TextBlock Text="{Binding Text}"></TextBlock> 
    </Underline> 
</Label> 

或者你也可以这样做,

<Label Name="label"> 
    <TextBlock Name="textBlock" TextDecorations="Underline" Text="Test"/> 
</Label> 

所以回到你定义内联文本 'Foo' 的下划线。

http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx

它是这样说的,

一个内联级流内容元素,它使内容以带下划线的文本装饰呈现。

所以它是一组内联,并采用这样的格式,

<Underline> 
  Inlines 
</Underline>