C# 将用户控件属性绑定到ItemsControl

C# 将用户控件属性绑定到ItemsControl,c#,wpf,xaml,binding,C#,Wpf,Xaml,Binding,我有以下UserControl: public partial class ConstraintBlock : UserControl { public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Constraint", typeof(Constraint) , typeof(ConstraintBlock)); publ

我有以下
UserControl

public partial class ConstraintBlock : UserControl
{
    public static readonly DependencyProperty LabelProperty =
        DependencyProperty.Register("Constraint", typeof(Constraint)
            , typeof(ConstraintBlock));

    public Constraint Constraint { get; set; }

    public event EventHandler EditClicked;

    public ConstraintBlock()
    {
        InitializeComponent();
    }

    private void btnEdit_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(Constraint.ToString());
        if (this.EditClicked != null) this.EditClicked(this, e);
    }
}
以下是它的XAML:

<UserControl x:Class="MyApp.Controls.ConstraintBlock"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyApp.Controls"
             mc:Ignorable="d" 
             d:DesignHeight="60" d:DesignWidth="500">
    <Grid Margin="5">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="25" />
                <RowDefinition Height="25" />
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <TextBlock x:Name="tbName" Grid.RowSpan="2" Text="{Binding Name}" />
            <Button x:Name="btnEdit" Grid.Row="1" Click="btnEdit_Click" />
        </Grid>
    </Grid>
</UserControl>

约束是定义如下的类:

namespace MyApp.Classes
{
    public class Constraint
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public ConstraintObject Object { get; set; }
        public ConstraintClause Clause { get; set; }
        public Nullable<ConstraintOperator> Operator { get; set; }
        public string Expression { get; set; }
    }

    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum ConstraintObject
    {
        Expression,
        [Description("File Extension")]
        FileExtension,
        [Description("File Name")]
        FileName
    }

    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum ConstraintClause
    {
        Contains,
        [Description("Does Not Contain")]
        DoesNotContain,
        Date,
        Length,
        Like,
        [Description("Not Like")]
        NotLike,
        Number
    }

    [TypeConverter(typeof(EnumDescriptionTypeConverter))]
    public enum ConstraintOperator
    {
        [Description("=")]
        EqualTo,
        [Description(">")]
        GreaterThan,
        [Description("<")]
        LessThan,
        [Description(">=")]
        GreaterThanOrEqualTo,
        [Description("<=")]
        LessThanOrEqualTo
    }
}
名称空间MyApp.class
{
公共类约束
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公共约束对象对象{get;set;}
公共约束原因子句{get;set;}
公共可空运算符{get;set;}
公共字符串表达式{get;set;}
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
公共枚举约束对象
{
表情,
[说明(“文件扩展名”)]
文件扩展,
[说明(“文件名”)]
文件名
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
公共枚举约束原因
{
包含,
[说明(“不包含”)]
不涉及,
日期,
长度,
喜欢
[说明(“不喜欢”)]
不象,
数
}
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
公共枚举约束运算符
{
[说明(“=”)]
埃奎尔托,
[说明(“>”)]
比,
[说明(“=”)]
比奥雷奎尔托更伟大,
[Description(“没有一个好的选项,很难说清楚。您没有显示
约束
类型是什么,也没有提供
约束块的实现

也就是说,我在代码中看到的最明显的问题是,您没有正确地实现
约束
依赖属性。您注册了该属性,但是getter和setter有其默认实现,而不是调用
GetValue()
SetValue()
。在不参与依赖属性系统的情况下,任何时候WPF试图直接通过
DependencyProperty
值而不是通过属性getter和setter使用该属性时,都不会发生任何有用的情况

这与获取
NullReferenceException
是一致的,假设
Constraint
是引用类型。因为依赖项属性的实际值在WPF方面保持为
null
,所以您会得到异常

如果我的理论正确,更改属性实现将解决问题:

public Constraint Constraint
{
    get { return (Constraint)GetValue(LabelProperty); }
    set { SetValue(LabelProperty, value); }
}

谢谢,Peter-它很有效!另外,为了清晰起见,我在中添加了约束类的定义。
public Constraint Constraint
{
    get { return (Constraint)GetValue(LabelProperty); }
    set { SetValue(LabelProperty, value); }
}