Click here to Skip to main content
Sign Up to vote bad
good
See more: C#XAMLWPF
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:
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):
<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 27 Sep '12 - 7:03

Comments
Sergey Alexandrovich Kryukov - 27 Sep '12 - 13:05
It has nothing to do with WPF at all. This is System.Windows.Data, is it not? --SA
NeonMika - 27 Sep '12 - 14:06
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:23
I see, thank you for the note. --SA

2 solutions

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.
  Permalink  
Comments
NeonMika - 27 Sep '12 - 14:10
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: 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:13
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:16
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!
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 514
1 CPallini 245
2 Mahesh Bailwal 244
3 Maciej Los 240
4 Aarti Meswania 213
0 Sergey Alexandrovich Kryukov 9,162
1 OriginalGriff 7,179
2 CPallini 3,913
3 Rohan Leuva 3,176
4 Maciej Los 2,588


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 30 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid