如何将多个值绑定到单个 WPF TextBlock?

发布于 2024-08-27 02:04:00 字数 310 浏览 9 评论 0原文

我当前正在使用下面的 TextBlock 来绑定名为 Name 的属性的值:

<TextBlock Text="{Binding Name}" />

现在,我想绑定名为 另一个 的属性>ID 到同一个 TextBlock

是否可以将两个或多个值绑定到同一个 TextBlock ?是否可以通过简单的串联来完成,例如Name + ID,如果不能,还有什么办法可以实现这一点?

I'm currently using the TextBlock below to bind the value of a property named Name:

<TextBlock Text="{Binding Name}" />

Now, I want to bind another property named ID to the same TextBlock.

Is it possible to bind two or more values to the same TextBlock? Can it be done with simple concatenation, like Name + ID and, if not, how else could this be approached?

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

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

发布评论

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

评论(4

浅沫记忆 2024-09-03 02:04:01

使用 ValueConverter

[ValueConversion(typeof(string), typeof(String))]
public class MyConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format("{0}:{1}", (string) value, (string) parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {

        return DependencyProperty.UnsetValue;
    }
}

并在标记中

<src:MyConverter x:Key="MyConverter"/>



<TextBlock Text="{Binding Name, Converter={StaticResource MyConverter Parameter=ID}}" />

Use a ValueConverter

[ValueConversion(typeof(string), typeof(String))]
public class MyConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Format("{0}:{1}", (string) value, (string) parameter);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {

        return DependencyProperty.UnsetValue;
    }
}

and in the markup

<src:MyConverter x:Key="MyConverter"/>

.
.
.

<TextBlock Text="{Binding Name, Converter={StaticResource MyConverter Parameter=ID}}" />
千年*琉璃梦 2024-09-03 02:04:00

您可以使用 MultiBinding< /a> 与 StringFormat< 结合使用/code>属性。用法类似于以下内容:

<TextBlock>
    <TextBlock.Text>    
        <MultiBinding StringFormat="{}{0} + {1}">
            <Binding Path="Name" />
            <Binding Path="ID" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Name 的值指定为 Foo,并将 ID 的值指定为 1,您的输出为TextBlock 将是 Foo + 1

注意:仅 .NET 3.5 SP1 和 3.0 SP2 或更高版本支持此功能。

You can use a MultiBinding combined with the StringFormat property. Usage would resemble the following:

<TextBlock>
    <TextBlock.Text>    
        <MultiBinding StringFormat="{}{0} + {1}">
            <Binding Path="Name" />
            <Binding Path="ID" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

Giving Name a value of Foo and ID a value of 1, your output in the TextBlock would then be Foo + 1.

Note: This is only supported in .NET 3.5 SP1 and 3.0 SP2 or later.

内心激荡 2024-09-03 02:04:00

我知道这已经晚了,但我想我应该添加另一种方法来做到这一点。

您可以利用这样一个事实:可以使用“Run<”来设置 Text 属性。 /a>s”,因此您可以为每个绑定使用“运行”来设置多个绑定。如果您无法访问 MultiBinding(我在为 Windows Phone 开发时没有找到),这非常有用

<TextBlock>
  <Run Text="Name = "/>
  <Run Text="{Binding Name}"/>
  <Run Text=", Id ="/>
  <Run Text="{Binding Id}"/>
</TextBlock>

I know this is a way late, but I thought I'd add yet another way of doing this.

You can take advantage of the fact that the Text property can be set using "Runs", so you can set up multiple bindings using a Run for each one. This is useful if you don't have access to MultiBinding (which I didn't find when developing for Windows Phone)

<TextBlock>
  <Run Text="Name = "/>
  <Run Text="{Binding Name}"/>
  <Run Text=", Id ="/>
  <Run Text="{Binding Id}"/>
</TextBlock>
埋葬我深情 2024-09-03 02:04:00

如果这些只是文本块(因此是一种方式绑定),并且您只想连接值,只需绑定两个文本块并将它们放入水平堆栈面板中。

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}"/>
        <TextBlock Text="{Binding ID}"/>
    </StackPanel>

这将显示文本(这是所有 Textblock 所做的),而无需进行任何更多编码。不过,您可以在它们上留一点边距,以使它们看起来正确。

If these are just going to be textblocks (and thus one way binding), and you just want to concatenate values, just bind two textblocks and put them in a horizontal stackpanel.

    <StackPanel Orientation="Horizontal">
        <TextBlock Text="{Binding Name}"/>
        <TextBlock Text="{Binding ID}"/>
    </StackPanel>

That will display the text (which is all Textblocks do) without having to do any more coding. You might put a small margin on them to make them look right though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文