Binding a unit label in WPF textbox

2019-05-23 03:12发布

问题:

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?

回答1:

You can use MultiBinding:

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