简体   繁体   中英

XAML MultiBinding StringFormat

I cannot figure out my MultiBinding StringFormat behavior. I have a DataContext which defines a numeric range via the properties MinIncl and MaxIncl. I want to create a tooltip using MultiBinding to create a tip like "1.0 to 999.0". I try the following code:

         <ToolTipService.ToolTip>
        <StackPanel>
           <TextBlock>
              <TextBlock.Text>
                 <MultiBinding StringFormat="{}{0:F1} to {1:F1}">
                    <Binding Path="SelectedTrainingScriptParameter.MinimumInclusive"/>
                    <Binding Path="SelectedTrainingScriptParameter.MaximumInclusive"/>
                 </MultiBinding>
              </TextBlock.Text>
           </TextBlock>
        </StackPanel>
     </ToolTipService.ToolTip>

The resulting tooltip is "to 999.0 1.0". So it has reversed the range and put the word "to" first.

If I remove the spaces and try "{}{0:F1}to{1:F1}", I get the right answer: "1.0to999.0".

Seriously, why does the whitespace and word "to" break this thing?

Thanks.

-reilly.

In this case you don't need a multi, just use the same TextBlock instead like;

<TextBlock>
   <Run Text="{Binding Path=SelectedTrainingScriptParameter.MinimumInclusive}"/>
   <Run Text="to"/>
   <Run Text="{Binding Path=SelectedTrainingScriptParameter.MaximumInclusive}"/>
</TextBlock>

Or if you really want to use it as is;

<TextBlock>
    <TextBlock.Text>    
        <MultiBinding StringFormat="{}{0} to {1}">
            <Binding Path="SelectedTrainingScriptParameter.MinimumInclusive" />
            <Binding Path="SelectedTrainingScriptParameter.MaximumInclusive" />
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>

As to the weird whitespace thing, no idea sorry. Hope this helps, cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM