【问题标题】:Applying a (non-multi) ValueConverter the output of a MultiBinding + StringFormat应用(非多)ValueConverter MultiBinding + StringFormat 的输出
【发布时间】:2014-08-02 16:02:57
【问题描述】:

有没有办法将(单个,而不是多个)ValueConverter 应用于使用 StringFormat 的 MultiBinding 的输出(即在字符串被格式化之后)。

它相当于那个代码,在其中我使用了一个中间折叠的 TextBlock 来完成这个技巧:

   <StackPanel>
        <TextBox x:Name="textBox1">TB1</TextBox>
        <TextBox x:Name="textBox2">TB2</TextBox>

        <TextBlock x:Name="textBlock" Visibility="Collapsed">
            <TextBlock.Text>
                <MultiBinding StringFormat="{}{0}{1}">
                    <Binding ElementName="textBox1" Path="Text"/>
                    <Binding ElementName="textBox2" Path="Text"/>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>

        <TextBlock Text="{Binding ElementName=textBlock,
                   Path=Text, Converter={StaticResource SingleValueConverter}}" />

    </StackPanel>

【问题讨论】:

    标签: wpf converter multibinding string-formatting


    【解决方案1】:

    这是一个可以做你想做的事情:

    public static class Proxy
    {
        public static readonly DependencyProperty TextProperty = DependencyProperty.RegisterAttached(
            "Text",
            typeof(string),
            typeof(Proxy),
            new PropertyMetadata(string.Empty));
    
        public static void SetText(this TextBlock element, string value)
        {
            element.SetValue(TextProperty, value);
        }
    
        [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
        [AttachedPropertyBrowsableForType(typeof(TextBlock))]
        public static string GetText(this TextBlock element)
        {
            return (string) element.GetValue(TextProperty);
        }
    }
    

    <StackPanel>
        <TextBox x:Name="textBox1">TB1</TextBox>
        <TextBox x:Name="textBox2">TB2</TextBox>
    
        <TextBlock Text="{Binding Path=(local:Proxy.Text), 
                                  RelativeSource={RelativeSource Self}, 
                                  Converter={StaticResource SingleValueConverter}}">
            <local:Proxy.Text>
                <MultiBinding StringFormat="{}{0}{1}">
                    <Binding ElementName="textBox1" Path="Text" />
                    <Binding ElementName="textBox2" Path="Text" />
                </MultiBinding>
            </local:Proxy.Text>
        </TextBlock>
    </StackPanel>
    

    【讨论】:

      【解决方案2】:

      如果您查看 MSDN 上的MultiBinding.Converter Property 页面,您会发现您可以MultiBinding 提供Converter。但是,它不是普通的IValueConverter,而是需要IMultiValueConverter。可以这样使用:

      <TextBlock x:Name="textBlock" Visibility="Collapsed">
          <TextBlock.Text>
              <MultiBinding StringFormat="{}{0}{1}" Converter="{StaticResource Converter}"
                  ConverterParameter="SomeValue">
                  <Binding ElementName="textBox1" Path="Text"/>
                  <Binding ElementName="textBox2" Path="Text"/>
              </MultiBinding>
          </TextBlock.Text>
      </TextBlock>
      

      可以在链接页面中找到IMultiValueConverter 实现的示例。

      【讨论】:

      • 抱歉,我想重用现有的单值转换器。我编辑了我的问题以澄清这一点。
      • 那么答案是否定的,你不能那样做。在新的IMultiValueConverter 中重现IValueConverter 的功能肯定是一项简单的任务?
      猜你喜欢
      • 2011-09-26
      • 2014-10-12
      • 2014-10-04
      • 2012-04-15
      • 1970-01-01
      • 2010-12-24
      • 2018-08-03
      • 2013-07-03
      • 2014-03-30
      相关资源
      最近更新 更多