Search code examples
c#wpfdata-bindingcode-behindivalueconverter

How to assign DisplayMemberBinding converter in the c# code


I have a GridViewColumn and I want to assign to its DisplayMemberBinding property a converter. I am able to do it in XAML, but I would like to add it from the code-behind for personals reasons.

When I try to assign my converter in the code it says that I need a IValueConverter type, but it is an interface and not an object that I can create. I am able to take the converter from an other GridViewColumn, but I won't always have the converter that I want assigned to another one.

<!-- XAML code that works and that I want to implement in the code-behind -->
<GridViewColumn x:Name="intensityColumnStatus" Header="Intensity" DisplayMemberBinding="{Binding Intensity,Converter={StaticResource converter}}" />

// Code-behind that I can't get to work
Binding visibilityBinding = new Binding("isPlaneComing");
visibilityBinding.Converter = ???;

I know that I'm maybe missing some information, so if you need more, please just ask :)


Solution

  • Alright, I figured it out. I was trying to just put

    visibilityBinding.Converter = myConverterClass;
    

    but in reality I needed to simply put

    visibilityBinding.Converter = new myConverterClass();
    

    I hope this can help someone in the future.