1

I want to do Binding to specific property, and make the checkbox converter according to the property values in class. I have an error.

This is my class:

namespace WpfApplication2
{
class Point
{
    public int point { get; set; }

    public Point(int x)
    {
        this.point = x;
    }
}
}

This is my Converter:

namespace WpfApplication2
{
    public class NumberToCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,     System.Globalization.CultureInfo culture)
        {
            if ((int)parameter >= 5)
                return true;
            return false;
        }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
}

This is my CS window's code:

namespace WpfApplication2

    public partial class MainWindow : Window
    {

    List<Point> points;
    public MainWindow()
    {
        InitializeComponent();
        points = new List<Point>();
        Random rnd = new Random();
        for (int i = 0; i < 10; i++)
        {
            points.Add(new Point(rnd.Next()));
        }

        this.DataContext = points;
    }
}
}

And this is the xaml:

Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication2"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <local:NumberToCheckedConverter x:Key="NumberToCheckedConverter"></local:NumberToCheckedConverter>

    <DataTemplate x:Key="MyDataTemplate"
              DataType="local:MyData">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="70" />
                <ColumnDefinition Width="70" />
            </Grid.ColumnDefinitions>
            <TextBox Text="Over 5" />
            <CheckBox Grid.Column="1" IsChecked="{Binding point, Converter={StaticResource NumberToCheckedConverter}, ConverterParameter=point}" IsEnabled="False" />
        </Grid>
    </DataTemplate>
</Window.Resources>

<Grid>
    <ListBox ItemTemplate="{StaticResource MyDataTemplate}"  ItemsSource="{Binding}" Height="172" HorizontalAlignment="Left" Margin="0,51,-0.2,0" Name="listBox1" VerticalAlignment="Top" Width="517" >
    </ListBox>
</Grid>

I have an error with the converter. What's wrong here?

Tal Tal
  • 13
  • 4

1 Answers1

1

A ConverterParameter is not a binding, so writing:

IsChecked="{Binding point, Converter={StaticResource NumberToCheckedConverter}, ConverterParameter=point}"

Is setting parameter to "point"; not really what you want. As it turns out, Converter Parameters aren't even Dependency Properties, and so cannot be bound.

However, you don't even need the parameter; just change your code to:

public class NumberToCheckedConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,     System.Globalization.CultureInfo culture)
        {
            if ((int)value >= 5)
                return true;
            return false;
        }

       public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
       {
           return Binding.DoNothing; //Null would cause an error on a set back.
       }
}

Converting the value will do what you want. If you wanted the threshold to be configurable, that is where ConverterParamater would come into play.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Just to make life even *more* confusing, `CommandParmeter` **is** a Dependency Property and can be bound normally. – BradleyDotNET Oct 16 '14 at 23:37
  • No, definitely **it is not**. Just as an example, check this: [link](http://stackoverflow.com/questions/15309008/binding-converterparameter) – Massimiliano Kraus May 17 '16 at 14:24
  • @MK87 Could you clarify your comment a bit? The link says ConverterParameter is not a DP, just like I do. My comment says **Command**Parameter is a DP, which it is. – BradleyDotNET May 17 '16 at 16:51
  • I'm sorry, I've not paid enough attention, so I've read "converter parameter", since that was the class this post was talking about. Yes, CommandParameter **is definitely** a DP. – Massimiliano Kraus May 18 '16 at 06:38