为什么带有转换器的多重绑定在工具提示中不起作用?

发布于 2024-11-14 12:16:36 字数 1723 浏览 3 评论 0原文

对于相当复杂的 WPF 工具提示的一部分,我尝试使用 MultiBinding 来生成基于两个属性的格式化文本。问题是,绑定的 MultiConverter 接收其 values 数组中每个项目的 DependencyProperty.UnsetValue

使用单个 Binding 可以实现以下效果:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <Binding Path="Amt" Converter="{StaticResource singleValueConverter}"/>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

使用带有 StringFormatMultiBinding 也是如此:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat='{0:C} in {1}'>
          <Binding Path="Amt"/>
          <Binding Path="Currency"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

但是 MultiBindingConverter 则不会:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <MultiBinding Converter="{StaticResource multiValueConverter}">
          <Binding Path="Amt"/>
          <Binding Path="Currency"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

最后一个示例中的绑定不会接收任何值。工具提示之外的情况并非如此 - 在这种特定情况下发生了什么导致绑定失败?

For part of a fairly-complex WPF ToolTip, I'm attempting to use a MultiBinding to produce formatted text based on two properties. The problem is, the binding's MultiConverter receives DependencyProperty.UnsetValue for each item in its values array.

The following works, using a single Binding:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <Binding Path="Amt" Converter="{StaticResource singleValueConverter}"/>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

And so does this, using a MultiBinding with StringFormat:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <MultiBinding StringFormat='{0:C} in {1}'>
          <Binding Path="Amt"/>
          <Binding Path="Currency"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

But a MultiBinding with a Converter does not:

<ToolTipService.ToolTip>
  <StackPanel>
    <TextBlock>
      <TextBlock.Text>
        <MultiBinding Converter="{StaticResource multiValueConverter}">
          <Binding Path="Amt"/>
          <Binding Path="Currency"/>
        </MultiBinding>
      </TextBlock.Text>
    </TextBlock>        
  </StackPanel>
</ToolTipService.ToolTip>

The bindings in the last example don't receive any value. This isn't the case outside of a ToolTip - what is going on such that binding fails in this specific case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-黛色若梦 2024-11-21 12:16:36

尝试在绑定上设置 Mode="OneWay"。

另外,您是否检查过此问题和解决方案:
http://social.msdn.microsoft.com/Forums/en-IE/wpf/thread/15ada9c7-f781-42c5-be43-d07eb1f90ed4

这个错误的原因是
工具提示尚未加载,所以
DependencyProperty.GetValue 返回
DependencyProperty.UnsetValue。你
应该添加一些代码来测试
值为 Dependency.UnsetValue。这
以下代码展示了如何执行此操作。

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) 
        return "";
    [...]
}

Try setting Mode="OneWay" on your binding.

Also, have you checked this problem and solution:
http://social.msdn.microsoft.com/Forums/en-IE/wpf/thread/15ada9c7-f781-42c5-be43-d07eb1f90ed4

The reason of this error is the
tooltips have not been loaded, so
DependencyProperty.GetValue returns
DependencyProperty.UnsetValue. You
should add some code to test that is
value is Dependency.UnsetValue. The
following code shows how to do this.

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
    if (values[0] == DependencyProperty.UnsetValue || values[1] == DependencyProperty.UnsetValue) 
        return "";
    [...]
}
甜中书 2024-11-21 12:16:36

试试这个:

<ToolTipService.ToolTip>
    <StackPanel>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource multiValueConverter}">
                    <MultiBinding.Bindings>
                        <BindingCollection>
                            <Binding Path="Amt"/>
                            <Binding Path="Currency"/>
                        </BindingCollection>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>        
    </StackPanel>
</ToolTipService.ToolTip>

Try this:

<ToolTipService.ToolTip>
    <StackPanel>
        <TextBlock>
            <TextBlock.Text>
                <MultiBinding Converter="{StaticResource multiValueConverter}">
                    <MultiBinding.Bindings>
                        <BindingCollection>
                            <Binding Path="Amt"/>
                            <Binding Path="Currency"/>
                        </BindingCollection>
                    </MultiBinding.Bindings>
                </MultiBinding>
            </TextBlock.Text>
        </TextBlock>        
    </StackPanel>
</ToolTipService.ToolTip>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文