Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
531 views
in Technique[技术] by (71.8m points)

c# - StringFormat and Multibinding with Label

I would like to use StringFormat to do someting like this :

<Label x:Name="myLabel">
    <Label.Content>
        <Multibinding StringFormat="{}{0} - {1}">
            <Binding Path="Lib1" />
            <Binding Path="Lib2" />
        </MultiBinding>
    </Label.Content>
</Label>

However, it's doesn't work and I got this error instead :

MultiBinding failed because it has no valid Converter. MultiBindingExpression:target element is 'Label' (Name='myLabel'); target property is 'Content' (type 'Object')

Is there any way to make this code work ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cant bind this because you are trying to bind a string to an object which wont work because StringFormat requires its target to be a string type. You can get around this by either using a TextBlock instead (which has a Text property) or putting the Textblock as the child of the Label:

<Label x:Name="myLabel">
    <Label.Content>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0} - {1}">
                    <Binding Path="Lib1" />
                    <Binding Path="Lib2" />
                 </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Label.Content>
</Label>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...