WPF liaison avec StringFormat ne fonctionne pas sur les info-bulles

le code suivant a une reliure simple qui lie le texte du TextBlock nommé MyTextBlock à la propriété TextBox et ToolTip en utilisant la même notation de reliure exacte:

<StackPanel>
    <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock>
    <TextBox    Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: {0}'}"
             ToolTip="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: {0}'}" />
</StackPanel>

la reliure utilise aussi la propriété StringFormat introduite avec .NET 3.5 SP1 qui fonctionne bien pour la propriété Text ci-dessus mais semble être cassée pour le ToolTip. Le résultat attendu est "it is: Foo Bar" mais lorsque vous survolez la TextBox, le ToolTip montre seulement la valeur de liaison, pas la valeur formatée de chaîne. Des idées?

75
demandé sur Will 2008-10-13 13:31:11

6 réponses

les infobulles dans WPF peuvent contenir n'importe quoi, pas seulement du texte, donc ils fournissent une propriété ContentStringFormat pour les moments où vous voulez juste du texte. Vous aurez besoin d'utiliser la syntaxe développée autant que je sache:

<TextBox ...>
  <TextBox.ToolTip>
    <ToolTip 
      Content="{Binding ElementName=myTextBlock,Path=Text}"
      ContentStringFormat="{}It is: {0}"
      />
  </TextBox.ToolTip>
</TextBox>

Je ne suis pas sûr à 100% de la validité de la liaison en utilisant la syntaxe ElementName à partir d'une propriété imbriquée comme celle-ci, mais la propriété ContentStringFormat est ce que vous recherchez.

138
répondu Matt Hamilton 2008-10-13 09:45:41

ça pourrait être un bug. Lorsque vous utilisez la syntaxe courte pour tooltip:

<TextBox ToolTip="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}" />

StringFormat est ignoré mais quand vous utilisez la syntaxe étendue:

<TextBox Text="text">
   <TextBox.ToolTip>
      <TextBlock Text="{Binding WhatEverYouWant StringFormat='It is: \{0\}'}"/>
   </TextBox.ToolTip>
</TextBox>

ça marche comme prévu.

15
répondu MuiBienCarlota 2014-07-31 09:54:28

comme Matt dit ToolTip peut contenir n'importe quoi à l'intérieur de sorte que pour votre vous pouvez lier une boîte de texte.Texte à l'intérieur de votre info-bulle.

<StackPanel>
    <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock>
    <TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}">
        <TextBox.ToolTip>
            <TextBlock>
                <TextBlock.Text>
                    <Binding ElementName=MyTextBlock Path="Text" StringFormat="It is: {0}" />
                </TextBlock.Text>
            </TextBlock>
        </TextBox.ToolTip>
    </TextBox>
</StackPanel>

même vous pouvez empiler une grille à l'intérieur de L'infobulle et mettre en page votre texte si vous voulez.

4
répondu Lucas Locatelli 2014-01-13 16:31:06

votre code peut être aussi court que ceci:

<TextBlock ToolTip="{Binding PrideLands.YearsTillSimbaReturns,
    Converter={StaticResource convStringFormat},
    ConverterParameter='Rejoice! Just {0} years left!'}" Text="Hakuna Matata"/>

nous allons utiliser le fait que les convertisseurs ne sont jamais ignorés, contrairement à StringFormat.

mettez ceci dans StringFormatConverter.cs :

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace TLKiaWOL
{
    [ValueConversion (typeof(object), typeof(string))]
    public class StringFormatConverter : IValueConverter
    {
        public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (ReferenceEquals(value, DependencyProperty.UnsetValue))
                return DependencyProperty.UnsetValue;
            return string.Format(culture, (string)parameter, value);
        }

        public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

mettez ça dans votre .xaml :

<conv:StringFormatConverter x:Key="convStringFormat"/>
2
répondu Athari 2012-12-25 22:01:07

dans cette situation, vous pouvez utiliser la liaison relative:

<StackPanel>
    <TextBlock x:Name="MyTextBlock">Foo Bar</TextBlock>
    <TextBox Text="{Binding ElementName=MyTextBlock, Path=Text, StringFormat='It is: \{0\}'}"
             ToolTip="{Binding Text, RelativeSource={RelativeSource Self}}" />
</StackPanel>
0
répondu Сергей Игнахин 2018-01-10 20:14:07

ce qui suit est une solution verbeuse mais elle fonctionne.

<StackPanel>
  <TextBox Text="{Binding Path=., StringFormat='The answer is: {0}'}">
    <TextBox.DataContext>
      <sys:Int32>42</sys:Int32>
    </TextBox.DataContext>
    <TextBox.ToolTip>
      <ToolTip Content="{Binding}" ContentStringFormat="{}The answer is: {0}" />
    </TextBox.ToolTip>
  </TextBox>
</StackPanel>

je préférerais une syntaxe beaucoup plus simple, quelque chose comme celle de ma question originale.

-7
répondu huseyint 2010-08-17 18:42:56