I have the following 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);
}
}
Here is the XAML for it:
<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>
A Constraint is a class defined as follows:
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
}
}
I then have the following ItemsControl in the UI:
<ItemsControl x:Name="constraintStack" ItemsSource="{StaticResource constraintCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<ctrls:ConstraintBlock Constraint="{Binding}" Grid.Row="2"
EditClicked="ConstraintBlock_EditClicked"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
When I add a Constraint to the constraintCollection resource, the ItemsControl shows the ConstraintBlock, and also shows the "Name" binding in the TextBlock as the "Name" (property) of the Constraint I just added.
The problem is that when I click the "Edit" button and btnEdit_Click() is called, I get a NullReferenceException on the MessageBox line - somehow the Constraint property of the ConstraintBlock object is null, even though I have (attempted to) set this with Constraint="{Binding}" in the XAML for the ItemsControl.
What is wrong with this binding?