Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello there!
I want to check if some number is greater than another and dispite that I want to make something visable or not.
It this exact case I want to check if my ListBox has Items. If not, my "Load more" button should not show up.

This is my converter, I'm pretty sure it is right written:
C#
namespace NeonMika.EightTracksPlayer
{
    public class GreaterIntegerToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if ( (int)value > (int)parameter )
                return Visibility.Visible;
            else
                return Visibility.Hidden;
        }

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

        #endregion
    }
}


And this is my Button with the Visibility-Binding to my ListBox (named MixControl):
XML
<Button Name="LoadMoreButton" HorizontalAlignment="Stretch" Click="LoadMoreButton_Click" Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter=0}">Load more</Button>


Can you see where my error is?
I really appreciate your help!

Thanks, Markus
Posted
Comments
Sergey Alexandrovich Kryukov 27-Sep-12 13:05pm    
It has nothing to do with WPF at all. This is System.Windows.Data, is it not?
--SA
NeonMika 27-Sep-12 14:06pm    
No, it has to do with WPF. This kind of converters are used in-line in XAML to convert different things. They are basic WPF constructs (Yeah, you can use them without WPF too, but that's not the idea behind them)
Sergey Alexandrovich Kryukov 27-Sep-12 16:23pm    
I see, thank you for the note.
--SA

Your XML reads:

Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter=0}">


Yet, the function that you want to call is:

NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter.Convert


So it seems to me your XML should read something like:

Visibility="{Binding ElementName=MixControl, Path=Items/Count, Converter={StaticResource NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter.Convert}, ConverterParameter=0}">


But even that seems a bit dubious, since you've declared GreaterIntegerToVisibilityConverter as a class and Convert as a non-static member of it.

So unless you are instantiating a GreaterIntegerToVisibilityConverter somewhere prior to that line of XML the Covert member function isn't accessible from anywhere.

And if you are instantiating one somewhere, it's not at all clear how the thing you instantiated elsewhere is actually bound to something that is known by the XML interpreter at the point it tries to evaluate Visibility.

Set a break point on your convert function and run it in the debugger -- I'm pretty sure it never gets called.

UPDATE:

Assuming you have something like this earlier in your XAML:

<local:NeonMika.EightTracksPlayer.GreaterIntegerToVisibilityConverter x:Key="GreaterIntegerConverter"/>


then you should be binding to the correct converter function -- if that's the case, the problem lies either with the element or the path.

Assuming the element is spelled right, then maybe there is an issue with the Path.

Try changing the path to just "Items" and have your converter extract the count from the collection.
 
Share this answer
 
v2
Comments
NeonMika 27-Sep-12 14:10pm    
That's the problem. It doesn't get called. I have some other self-written converters that work without problems.
And yes, in my app.xaml's Resources I have my Converters created.
Like this:
<local:greaterintegertovisibilityconverter x:key="GreaterIntegerConverter">

I think another thing I could try is to change the Path to Items.Count, but I used the VS "Binding-Wizard" who wrote this line.

Here an example on converters: http://www.codeproject.com/Articles/24330/WPF-Bind-to-Opposite-Boolean-Value-Using-a-Convert
TRK3 27-Sep-12 14:13pm    
If you have other self written converters that work, then what's the difference between the ones that work and the ones that don't?

(What ever example you tried to put in your comment above isn't showing up.)
NeonMika 27-Sep-12 14:16pm    
I'm now @ my girlfriends place and not at home till tomorrow.
The differences are the this is the only one who is refering to another element (others link to their datacontext) and this one is using a number instead of string. So I trie another Path-writing-method tomorrow and will try to but the 0 in "". I hope it will work then..
Ok, I got it working now.
I don't now why the Visual Studio Binding Assistent (by using the Properties List) builds the Path to the Property with "/", but it has to be a "."
To give a little more information: If you use a ConvertParameter (like me), this parameter should be put in 'abc'. This ensures the parameter is recognized as string. But you can also use another binding here (if your parameter is depending on another Property).

Now my working code is the following:

Declaration in the Window.Resources (or App.xaml, etc.):
<local:greaterintegertovisibilityconverter x:key="GreaterIntegerConverter" xmlns:x="#unknown" xmlns:local="#unknown"></local:greaterintegertovisibilityconverter>


Then this for the control:
<button name="LoadMoreButton" horizontalalignment="Stretch" click="LoadMoreButton_Click" visibility="{Binding ElementName=MixControl, Path=Items.Count, Converter={StaticResource GreaterIntegerConverter}, ConverterParameter='0'}">Load more</button>


And this is the working code for the converter:
public class GreaterIntegerToVisibilityConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        try
        {
            int val = System.Convert.ToInt32(value);
            int para = System.Convert.ToInt32(parameter);

            if ( val > para )
                return Visibility.Visible;
            else
                return Visibility.Hidden;
        }
        catch ( Exception ex )
        {
            return Visibility.Hidden;
        }
    }

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


Thanks for your ideas!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900