在WPF文本框中绑定单位标签 [英] Binding a unit label in WPF textbox

查看:243
本文介绍了在WPF文本框中绑定单位标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将压力绑定到文本框.压力可以是PSI,KPA,BAR等.我想在值旁边的文本框中显示单位.似乎没有一种方法可以将我的viewmodel中的Units字符串属性绑定到值绑定的StringFormat选项.有什么方法可以完成而无需重新模板化文本框?

I'm binding a pressure to a textbox. The pressure could be in PSI, KPA, BARS, etc. I would like to display the unit inside the textbox next to the value. There doesn't seem to be a way to bind the Units string property in my viewmodel to the StringFormat option of the value binding. Is there any way to accomplish this without retemplating the textbox?

推荐答案

您可以使用MultiBinding:

<TextBox>
    <TextBox.Text>
        <MultiBinding StringFormat="{}{0} {1}">
            <Binding Path="Pressure" />
            <Binding Path="Unit"/>
        </MultiBinding>
    </TextBox.Text>
</TextBox>

这篇关于在WPF文本框中绑定单位标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆