Programing

StringFormat을 사용한 WPF 바인딩이 도구 설명에서 작동하지 않습니다.

lottogame 2020. 10. 5. 07:30
반응형

StringFormat을 사용한 WPF 바인딩이 도구 설명에서 작동하지 않습니다.


다음 코드에는 정확히 동일한 Binding 표기법을 사용하여 MyTextBlock이라는 TextBlock의 Text를 TextBox의 Text 및 ToolTip 속성에 바인딩하는 간단한 바인딩이 있습니다.

<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>

바인딩은 또한 .NET 3.5 SP1에 도입StringFormat 속성을 사용합니다.이 속성 은 위의 Text 속성에 대해 잘 작동하지만 도구 설명에 대해서는 손상된 것 같습니다. 예상 결과는 "It is : Foo Bar"이지만 TextBox 위로 마우스를 가져 가면 도구 설명에 문자열 형식 값이 아닌 바인딩 값만 표시됩니다. 어떤 아이디어?


WPF의 도구 설명은 텍스트뿐만 아니라 모든 것을 포함 할 수 있으므로 텍스트를 원하는 시간에 ContentStringFormat 속성을 제공합니다. 내가 아는 한 확장 구문을 사용해야합니다.

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

이와 같은 중첩 속성에서 ElementName 구문을 사용하여 바인딩의 유효성에 대해 100 % 확신 할 수는 없지만 ContentStringFormat 속성이 찾고있는 것입니다.


버그 일 수 있습니다. 툴팁에 짧은 구문을 사용하는 경우 :

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

StringFormat은 무시되지만 확장 구문을 사용하는 경우 :

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

예상대로 작동합니다.


Matt가 말했듯이 ToolTip은 내부에 모든 것을 포함 할 수 있으므로 ToolTip 내부에 TextBox.Text를 바인딩 할 수 있습니다.

<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>

원하는 경우 도구 설명 안에 그리드를 쌓고 텍스트를 레이아웃 할 수도 있습니다.


코드는 다음과 같이 짧을 수 있습니다.

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

StringFormat과 달리 변환기는 무시되지 않는다는 사실을 사용합니다.

이것을 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();
        }
    }
}

이것을 ResourceDictionary.xaml에 넣으십시오 .

<conv:StringFormatConverter x:Key="convStringFormat"/>

이 상황에서 상대 바인딩을 사용할 수 있습니다.

<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>

The following is a wordy solution but it works.

<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>

I would prefer a much simpler syntax, something like the one in my original question.

참고URL : https://stackoverflow.com/questions/197095/wpf-binding-with-stringformat-doesnt-work-on-tooltips

반응형