StringFormat and Multibinding with Label

前端 未结 3 658
情深已故
情深已故 2020-12-15 15:34

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

相关标签:
3条回答
  • 2020-12-15 16:09
    <Label>
       <AccessText>
          <MultiBinding StringFormat="{x:Static properties:Resources.MyText}">
             <Binding Path="MyObj.MyProp" Mode="OneTime"/>
          </MultiBinding>
       </AccessText>
    </Label>
    

    Where Resources.MyText can hold anything like "Fox jumps over {0}."

    0 讨论(0)
  • 2020-12-15 16:12

    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>
    
    0 讨论(0)
  • 2020-12-15 16:23

    For those wondering you can also leave the <Label.Content> tag from Leom Burke's answer. This saves another two lines of code.

    <Label x:Name="myLabel">
        <TextBlock>
            <TextBlock.Text>
               <MultiBinding StringFormat="{}{0} - {1}">
                   <Binding Path="Lib1" />
                   <Binding Path="Lib2" />
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>
    </Label>
    
    0 讨论(0)
提交回复
热议问题