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

发布于 2024-07-18 16:58:55 字数 236 浏览 4 评论 0原文

在 WPF 中,有没有办法让 TextBlockText 属性包含硬编码文本和特定绑定?

我的想法大致如下(当然,下面的内容无法编译)

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

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伏妖词 2024-07-25 16:58:55

如果您使用的是 .Net 3.5 SP1,则有

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

There is, if you are on .Net 3.5 SP1

<TextBlock Text="{Binding Path=Artist.Fans.Count, 
                 StringFormat='Number of Fans: {0}'}" />
┊风居住的梦幻卍 2024-07-25 16:58:55

在使用上述方法时:

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

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

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

<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>                    

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.

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>                    
开始看清了 2024-07-25 16:58:55

使用 Binding.StringFormat

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

Use Binding.StringFormat:

<TextBlock Text="{Binding Artist.Fans.Count, StringFormat='Number of Fans: {0}'}"/>
神爱温柔 2024-07-25 16:58:55

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

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

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

 <TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}%}"/>
何必那么矫情 2024-07-25 16:58:55

使用模板 10 和 MVVM 的 XAML:

需要明确的是:

  • 根据定义,绑定将值绑定到控件的属性。
  • 在“Template 10”框架中实现的 MVVM 范例下,这些值在与相关 XAML 页面关联的 ViewModel 中初始化。

以下是如何将硬编码文本与 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>

With XAML using Template 10 and MVVM:

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.

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

    <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>
后知后觉 2024-07-25 16:58:55

对我有用的解决方案:

<Label Content="{Binding Artist.Fans.Count}" ContentStringFormat="Number of {0}"/>

The solution that worked for me:

<Label Content="{Binding Artist.Fans.Count}" ContentStringFormat="Number of {0}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文