Silverlight 4 绑定到 ConverterParameter [英] Silverlight 4 Binding to ConverterParameter

查看:16
本文介绍了Silverlight 4 绑定到 ConverterParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ValueConverter,需要根据属性使用动态参数调用它.我看不出有什么办法做到这一点......

I have a ValueConverter which needs to be called with a dynamic parameter, depending on a property. I can't see a way to do this ...

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

分数"转换器获取(或应该获取)System.Size 类型的参数,该参数包含分子和分数.这个值(应该)取决于一个 ItemCollection.Count.重置 ItemCollection 应使用新值重新调用 Converter.

The "Fraction" converter get's (or should get) a parameter of type System.Size, which contains a numerator and denumerator. This value (should) depend on a ItemCollection.Count. Resetting the ItemCollection should reinvoke the Converter with the new values.

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

My first idea was to manually change the ConverterParameter in CodeBehind on the PropertyChanged event of my ItemCollection DependencyProperty. But, as I know now, Silverlight has no GetBinding() method. I heard about GetBindingExpression and tried to do. But MyGrid.GetBindingExpression(Grid.ActualHeightProperty) is always returning null, although the Binding is already established.

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

So, what can I do to reach my target?

我的实现没有太大不同.在通过绑定调用转换器之前,我在 CodeBehind 中设置了 ConverterParameter.这不起作用(参数仍包含初始化值).

My implementation was not much different. I set the ConverterParameter in CodeBehind just before the Converter is called via Binding. That hasn't worked (Parameter contains still the initialization value).

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

I'll try to use your suggestion. But why ConverterParameter can't be a DependencyPropery. What's the idea behind this? Does anybody know?

推荐答案

If..
您所说的取决于属性"是指除了 ActualWidthValue 之外,DataContext 还有另一个属性,您需要使用它来计算要分配给 Width
的值..然后:

If..
what you mean by "depending on a property" is that there is another property of the DataContext apart from ActualWidthValue which you need in order to calculate the value you want to assign to Width
..then:

修改您称为Fraction"的 IValueConverter 以获取整个对象.它可以获取ActualWidthValue 的值以及它需要的任何其他值,然后返回所需的宽度.

Modify the IValueConverter you call "Fraction" to take the entire object instead. It can the acquire the value of ActualWidthValue and any other values it needs and then return the required width.

编辑

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

From your comment I see the my first "if.." paragraph is false. You actually have a common value across the UserControl that this converter should be using. In this case add a property to the converter, it is after all just another class. When the property on the UserControl is set you assign its value to this property. For example:-

一些价值转换器:-

 public class SomeConverter : IValueConverter
 {

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

用户控件 xaml:-

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

这篇关于Silverlight 4 绑定到 ConverterParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆