【问题标题】:Silverlight 4 Binding to ConverterParameterSilverlight 4 绑定到 ConverterParameter
【发布时间】:2011-05-29 09:40:52
【问题描述】:

我有一个 ValueConverter 需要使用动态参数调用,具体取决于属性。我看不到这样做的方法...

Width="{Binding ActualWidthValue, Source={StaticResource VisibleSize}, Converter={StaticResource Fraction}}"

“分数”转换器获取(或应该获取)System.Size 类型的参数,其中包含分子和分母。该值(应该)取决于 ItemCollection.Count。重置 ItemCollection 应该使用新值重新调用 Converter。

我的第一个想法是在我的 ItemCollection DependencyProperty 的 PropertyChanged 事件上手动更改 CodeBehind 中的 ConverterParameter。但是,正如我现在所知,Silverlight 没有 GetBinding() 方法。 我听说过 GetBindingExpression 并尝试去做。但是 MyGrid.GetBindingExpression(Grid.ActualHeightProperty) 总是返回 null,尽管 Binding 已经建立。

那么,我该怎么做才能达到我的目标?


我的实现并没有太大的不同。在通过 Binding 调用 Converter 之前,我在 CodeBehind 中设置了 ConverterParameter。这不起作用(参数仍然包含初始化值)。

我会尝试使用您的建议。但是为什么 ConverterParameter 不能是 DependencyPropery。这背后的想法是什么?有人知道吗?

【问题讨论】:

    标签: silverlight dynamic binding converter


    【解决方案1】:

    听起来您可能想要使用 MultiBinding。这是 WPF 的一项功能,但与很多东西一样,还有 3rd 方实现:

    http://www.scottlogic.co.uk/blog/colin/2010/05/silverlight-multibinding-solution-for-silverlight-4/

    然后您可以将 ActualWidthValue 和项目集合计数作为单独的参数传入。

    【讨论】:

    • 感谢您的回答。我知道 MultiBinding 并且已经有了一个有效的实现。但这并不重要。我只需要一个值作为参数。我的问题是,我无法将 ConverterParameter 与绑定一起使用。任何 StaticResource 都可以正常工作。有没有可能与 ConverterParameter 建立绑定?
    • 如果我使用绑定:Width="{Binding ActualWidthValue, Source={StaticResource VisibleSize}, Converter={StaticResource Fraction}, ConverterParameter={Binding ElementName=ColumnFraction}}" 然后我得到消息“价值不在预期范围内”......完全空洞 - 在我看来。
    • @FrEEzE2046:您不能对 ConverterParameter 使用绑定。绑定的目标(即值设置为{Binding ...} 的属性)必须是依赖属性,但 Binding.ConverterParameter 不是依赖属性。至于无用的错误消息,......好吧,我们只是说这是 Silverlight 的一个方面,可以做一些改进......
    【解决方案2】:

    如果..
    “取决于一个属性”的意思是,除了 ActualWidthValue 之外,DataContext 还有另一个属性,您需要它来计算要分配给 Width 的值
    ..然后:

    修改您称为“Fraction”的 IValueConverter 以取而代之的是整个对象。它可以获取ActualWidthValue的值和它需要的任何其他值,然后返回所需的宽度。

    编辑

    从您的评论中,我看到我的第一个“如果..”段落是错误的。实际上,这个转换器应该使用的 UserControl 中有一个共同的值。在这种情况下,向转换器添加一个属性,它毕竟只是另一个类。当设置 UserControl 上的属性时,您将其值分配给该属性。例如:-

    一些值转换器:-

     public class SomeConverter : IValueConverter
     {
    
         public int SomeFactor { get; set }
         // IValueConverter implementation here uses SomeFactor
     }
    

    UserControl xaml:-

     <UserControl.Resources>
        <local:SomeConverter x:Key="Fraction" SomeFactor="15" />
     </UserControl.Resources>
    

    用户控制代码隐藏:-

     public int SomeFactor
     {
          get { return ((SomeConverter)Resources["Fraction"]).SomeFactor; }
          set { ((SomeConverter)Resources["Fraction").SomeFactor = value; }
     }
    

    【讨论】:

    • 对,我需要另一个值——除了 ActualWidthValue——来计算正确的输出值。我想将此值作为我的 Convert() 方法的参数传递。系数 - 可以这么说 - 是我的 UserControl 的属性。 ActualWidthValue 是我使用的 StaticResource 的一个属性。那么,如何修改我的 IValueConverter 以获取整个 UserControl 和我的 ActualWidthValue?
    【解决方案3】:

    也许你可以做到:

    <Image.Width>
    <Binding Path="Rank" Converter="{StaticResource RankWidthConverter}" ConverterParameter="{Binding Path=Width, ElementName=barBorder}" />
    </Image.Width>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2011-03-13
      相关资源
      最近更新 更多