问题描述
我有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,其中包含一个分子和denumerator的参数。此值(应)取决于ItemCollection.Count。重置ItemCollection应该重新调用转换器使用新值。
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.
我的第一个想法是手动更改codeBehind的ConverterParameter我ItemCollection DependencyProperty的PropertyChanged事件。但是,正如我现在知道了,Silverlight中没有GetBinding()方法。
我听说GetBindingEx pression,并试图做的。但MyGrid.GetBindingEx pression(Grid.ActualHeightProperty)总是返回null,虽然绑定已经建立。
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?
推荐答案
如果..结果
你的意思是取决于财产什么是有在DataContext的另一个属性除了 ActualWidthValue
您以计算要分配给宽度值需要结果
..then:
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:
修改你所说的分数的的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.
修改
从您的评论我看到了我的第一个如果......一段是假的。实际上,你必须在整个用户控件一个共同的价值,这种转换应该使用。在这种情况下,属性添加到转换器,它毕竟只是一个类。当在用户控件的属性设置你它的值赋给这个属性。例如: -
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:-
某个值转换器: -
Some Value converter:-
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>
用户控件codeBehind: -
UserControl CodeBehind:-
public int SomeFactor
{
get { return ((SomeConverter)Resources["Fraction"]).SomeFactor; }
set { ((SomeConverter)Resources["Fraction").SomeFactor = value; }
}
这篇关于Silverlight 4中绑定到ConverterParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!