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
988 views
in Technique[技术] by (71.8m points)

wpf - String format using MultiBinding?

I'm trying to display a string in XAML using Label control. Following is my XAML code :

<Label Height="28" HorizontalAlignment="Left" Margin="233,68,0,0" Name="label13" VerticalAlignment="Top">
    <Label.Content>
        <MultiBinding StringFormat="{}{0} x {1}">
              <Binding Path="Width" />
              <Binding Path="Height" />
        </MultiBinding>
    </Label.Content>

Width and Height are two properties of my class Movie. I want the label to display : "Width x Height" ex. 800 x 640 However the label control remains empty. Any help is appreciated. I WANT TO DO THIS WITHOUT USING A CONVERTER.


I have modified my xaml by using a TextBlock instead of Label. But still it wont populate display the output.

<TextBlock Height="28" HorizontalAlignment="Left" Margin="233,68,0,0" Name="label13" VerticalAlignment="Top">
                <TextBlock.Text>
                    <MultiBinding StringFormat="{}{0} x {1}">
                        <Binding Path="Width" />
                        <Binding Path="Height" />
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

you are trying to bind a string to an object. But StringFormat requires its target to be a string type.

try putting a TextBlock in your label content and bind your data to it

<StackPanel>
  <Slider x:Name="sl1" Minimum="10" Maximum="100"/>
  <Slider x:Name="sl2" Minimum="10" Maximum="100"/>
  <Label x:Name="label13" Background="Yellow" Foreground="Black">
    <Label.Content>
        <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="{}{0} x {1} Test">
              <Binding ElementName="sl1" Path="Value" />
              <Binding ElementName="sl2" Path="Value" />
            </MultiBinding>
          </TextBlock.Text>
        </TextBlock>
    </Label.Content>
  </Label>
</StackPanel>

EDIT your class Movie must implement the INotificationPropertyChanged interface and your two properties must raise the property changed event with their proprty names!

hope this helps


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

...