wpf 在 TextBlock 中使用绑定硬编码文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/789222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:17:44  来源:igfitidea点击:

Having hardcoded text with a binding in a TextBlock

wpfxamldata-bindingtextblock

提问by Andreas Grech

In WPF, is there any way to have the Textproperty of a TextBlockto contain both hard coded text and a specific binding?

在 WPF 中,有没有办法让Texta的属性TextBlock同时包含硬编码文本和特定绑定?

What I have in mind is something along the lines of the following (ofcourse, the below doesn't compile):

我想到的是以下内容(当然,以下内容无法编译)

<TextBlock Text="Number of Fans: {Binding Artist.Fans.Count}"></TextBlock>

回答by Scott Weinstein

There is, if you are on .Net 3.5 SP1

有,如果您使用 .Net 3.5 SP1

<TextBlock Text="{Binding Path=Artist.Fans.Count, 
                 StringFormat='Number of Fans: {0}'}" />

回答by doogie

In using the above approach:

在使用上述方法时:

<TextBlock Text="{Binding Path="Artist.Fans.Count, 
                  StringFormat='Number of Fans: {0}'}" />

I found it somewhat restrictive in that I couldn't find a way to bold face inside the StringFormat nor could I use an apostrophe in the StringFormat.

我发现它有些限制,因为我找不到在 StringFormat 中加粗的方法,也无法在 StringFormat 中使用撇号。

Instead I went with this approach, which worked better for me:

相反,我采用了这种方法,这对我来说效果更好:

<TextBlock TextWrapping="Wrap">
    <Run>The value</Run>
    <Run Text="{Binding Path=MyProperty1, Mode=OneWay}" FontWeight="Bold" />
    <Run>was invalid. Please enter it with the format... </Run>
    <LineBreak/><LineBreak/>
    <Run>Here is another value in the program</Run>
    <Run Text="{Binding Path=MyProperty2, Mode=OneWay}" FontWeight="Bold" />
</TextBlock>                    

回答by Danko Durbi?

Use Binding.StringFormat:

使用Binding.StringFormat

<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>

回答by reza.cse08

Here the binding value(clouds.all) is added with "%". You can add any value you want after "\{0\}".

这里绑定值(clouds.all)添加了“%”。您可以在“\{0\}”之后添加您想要的任何值。

 <TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>

回答by Varus Septimus

With XAML using Template 10 and MVVM:

使用模板 10 和 MVVM 的 XAML:

Just to be clear:

只是要清楚:

  • By definition, binding binds values to properties of controls.
  • Under the MVVM paradigm as implemented in the 'Template 10' framework, the values are initialized in the ViewModel associated to the relevant XAML page.
  • 根据定义,绑定将值绑定到控件的属性。
  • 在“模板 10”框架中实现的 MVVM 范式下,值在与相关 XAML 页面关联的 ViewModel 中初始化。

Here is how to have hardcoded text together with a binding in a Text property:

以下是如何将硬编码文本与 Text 属性中的绑定一起使用:

    <Page
        ...
        xmlns:vm="using:doubleirish.ViewModels"
        xmlns:sys="using:System"
        xmlns:controls="using:Template10.Controls"
        ...

        <Page.DataContext>
            <vm:StocksViewModel x:Name="ViewModel" />
        </Page.DataContext>

        ...

        <controls:PageHeader ... Text="{x:Bind sys:String.Format('Ticker : {0}', ViewModel.Ticker)}">

    ...

    </Page>