1

I'm trying to make a custom converter that inherits from DependencyObject, but it doesn't work:

Converter:

public class BindingConverter : DependencyObject , IValueConverter
{
  public object Value
  {
    get { return (object)GetValue(ValueProperty); }
    set { SetValue(ValueProperty, value); }
  }
  public static readonly DependencyProperty ValueProperty =
      DependencyProperty.Register("Value", typeof(object), typeof(BindingConverter), new PropertyMetadata(null));


  public object Convert(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
  {
    Debug.Assert(Value != null); //fails
    return Value;
  }

  public object ConvertBack(object value, Type targetType, object parameter, Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

Xaml:

<StackPanel x:Name="this">
  <!--works-->
  <ContentControl Content="{Binding ActualHeight, ElementName=this}"/>
  <!--doesn't work-->
  <ContentControl>
    <Binding>
      <Binding.Converter>
        <BindingConverter Value="{Binding ActualHeight, ElementName=this}" />
      </Binding.Converter>
    </Binding>
  </ContentControl>
  <TextBlock Text="{Binding Animals}"/>
</StackPanel>

Am I missing out anything?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

3 Answers3

1

I have some places in my projects where I needed similar functionality. Can't show you exact sample, just an idea:

  • perhaps you have to inherit from FrameworkElement, not IValueConverter, Something like this:

    public class BindingHelper : FrameworkElement    
    
  • in the BindingHelper class, set Visibility to Collapsed and IsHitTestVisible to false;

  • to make it working, insert it into visual tree directly. In your example, it should be a child of the StackPanel. So, it will have the same DataContext as other StackPanel children;
  • then, you can add one ore more dependency properties depending on your needs. For example, you might have single property for the source of data and some different properties which you then will use as converter return values. Handle all changes to the source property in your BindingHelper class and change output properties accordingly;
  • bind other controls to properties of the BindingHelper class using ElementName syntax
notacat
  • 671
  • 6
  • 11
0

Note! ActualHeight property's binding is buggy on binding!

Why you inherit DependencyObject when coding a converter? You should just implement IValueConverter.

Try that,

First add MyConverter by the key of "MyConverterResource" on your resources then, You can do than on XAML side or on cs side by

//You may do it on XAML side <UserControl.Resources>...
this.Resources.Add("MyConverterResource",new MyConverter());

<TextBlock Text="{Binding ActualHeight,ElementName=this
,Converter=MyConverterResource}"/>

public class MyConverter: IValueConverter
{

public object Convert(object value, Type targetType
, object parameter,Globalization.CultureInfo culture)
 {

   return "Your Height is:"+Value.toString();
}

}

Hope helps

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Davut Gürbüz
  • 5,526
  • 4
  • 47
  • 83
  • first of all, sorry for choosing ActualHeight/Width for my example. In my real world example I'm trying to implement a converter that mimics the functionality of the WPF's `{RelativeSource PreviuosData}`. So to do this I want to pass along with the binding, a reference to the collection. I tried to bind with `{Binding Converter={PreviousDataConverter Collection={Binding ItemsSource, ElementName=myItemsControl}}}`. – Shimmy Weitzhandler Mar 29 '12 at 00:49
  • I think I got the question now , you try to show the current and previous data of a source together on UI.If I'm right, You may reach this feature by using Converter parameter. I didn't need this feautre before,but for some converters I pass parameter on Convert method for different responses. {Binding Source=itemssouce,converter=preconverter,converterparameter={Binding ElementName=X,Path=index?}, ... something like this? – Davut Gürbüz Mar 29 '12 at 07:37
  • That's for sure not gonna work, the `Parameter` converter is not bindable AFAIK. – Shimmy Weitzhandler Mar 29 '12 at 15:28
  • I got it , here is a soulution for passing parameter http://blog.andrew-veresov.com/post/Binding-a-Converter-Parameter.aspx – Davut Gürbüz Mar 30 '12 at 06:34
  • No. I want the parameter to be a bound value, not a static resource. – Shimmy Weitzhandler Mar 31 '12 at 18:43
  • If read the previous link I shared, there is a solution without using parameter prop., Andrew's formule is an interface solution, on your itemssource side implement an interface i.e. gives us previous and next values. Then on the side IValueConverter implemented you just test if your value implemented your source's interface,cast your value object to this interface and get next and previous data. I know binding parameter property is easier but silverlight doesn't support binding on it. – Davut Gürbüz Apr 02 '12 at 07:50
  • i appreciate your efforts very much, but I didn't find a solution in that post. Can you quote how to extract previous/next binding from current binding? – Shimmy Weitzhandler Apr 02 '12 at 21:13
0

in Silverlight, ActualHeight and ActualWidth properties don't do notifications on property updates. So, binding to them won't work.

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
notacat
  • 671
  • 6
  • 11