从类属性转换器

时间:2014-10-16 23:22:51

标签: c# wpf xaml ivalueconverter

我想对特定属性进行绑定,并根据类中的属性值创建复选框转换器。 我有一个错误。

这是我的班级:

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

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

这是我的转换器:

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;
    }
}
}

这是我的CS窗口代码:

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;
    }
}
}

这就是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>

转换器出错了。这里有什么问题?

1 个答案:

答案 0 :(得分:1)

ConverterParameter不是绑定,所以写道:

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

将参数设置为“point”;不是你想要的。事实证明,转换器参数甚至不是依赖属性,因此无法绑定。

但是,你甚至不需要参数;只需将您的代码更改为:

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.
       }
}

转换价值会做你想要的。如果您希望阈值可配置,那么ConverterParamater就会发挥作用。