Search code examples
wpfbindingivalueconverter

How to bind the ConverterParameter property of a binding to a DependencyProperty


I'm building a UserControl for my WPF application which will allow the user to navigate through pages of data returned by a search. I have to reduce my application's memory usage and, depending on the critieria, a search can return lots of data.

The window contains a Telerik RadGridView control now and will contain an instance of this new UserControl. The new control will have buttons for going back to the first page of data, the previous page, another for the next page, and one more for the last page, as well as a ComboBox for going to a particular page number.

I want to enable or disable the buttons such that the first page button and the previous page button are enabled only if the current page isn't the first, and the next page button and the last page button are enabled only if the current isn't the last.

Now, I have created two classes that implement IValueConverter in my application which do comparisons. They compare the value parameter to the Parameter argument in the Convert method. This works OK for the First and Previous Page buttons, since I'm comparing to zero. It's the Next and Last Page buttons where this may fall apart.

The UserControl has a DependencyProperty that represents the number of pages of data that match the query criteria. I have an IValueConverter that returns true if the value argument is less than the parameter argument. Can I bind the ConvertParameter property of the Binding to the number pages DependencyProperty? Or do I have to add another property that indicates it's ok to enable these buttons?


Solution

  • You are trying to abuse the IValueConverter to convert multiple values to a single one. Use an implementation of IMultiValueConverter for this and use it like so.

        <MultiBinding Converter="{your IMultiValueConverter implementation}"
                      Mode="OneWay">
            <Binding Path="Property1" />
            <Binding Path="Property2" />
        </MultiBinding>
    

    http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx