Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

dynamic - Silverlight 4 Binding to ConverterParameter

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...