Silverlight 4 绑定到 ConverterParameter

发布于 2024-10-08 22:15:17 字数 813 浏览 2 评论 0原文

我有一个 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。这背后的想法是什么?有人知道吗?

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

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.

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?


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).

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

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

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

发布评论

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

评论(3

○愚か者の日 2024-10-15 22:15:17

如果..
“取决于属性”的意思是,除了 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>

UserControl CodeBehind:-

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

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:

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.

Edit

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
 }

UserControl xaml:-

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

UserControl CodeBehind:-

 public int SomeFactor
 {
      get { return ((SomeConverter)Resources["Fraction"]).SomeFactor; }
      set { ((SomeConverter)Resources["Fraction").SomeFactor = value; }
 }
笑看君怀她人 2024-10-15 22:15:17

听起来您可能想使用多重绑定。这是一个 WPF 功能,但与许多其他功能一样,也有第 3 方实现:

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

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

Sounds like you might want to be using a MultiBinding. It's a WPF feature, but as with a lot of things there's 3rd party implementations:

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

You could then pass in the ActualWidthValue and the item collection count in as separate parameters.

青春如此纠结 2024-10-15 22:15:17

也许你可以这样做:

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

Perhaps You Could Do:

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