に私はUserControl
を次のようしている。ここでバインドユーザーコントロールのプロパティのItemsControl
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
}
}
次に、UIに次のItemsControlがあります。
<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>
IはconstraintCollection
リソースにConstraint
を追加すると、ItemsControl
はConstraintBlock
を示しており、また、私はちょうど追加Constraint
の「名前」(プロパティ)として「名前」TextBlock
で結合を示します。
問題は、私は、「編集」ボタンをクリックして、btnEdit_Click()
が呼び出されたとき、私はMessageBox
ライン上NullReferenceException
得るということである - 何とかConstraintBlock
オブジェクトのConstraint
プロパティは、私が(しようと)しているにもかかわらず、nullのセットこれはItemsControl
のXAMLにConstraint="{Binding}"
となります。
このバインディングで何が問題になっていますか?
おかげで、ピーター - それは働きます!また、わかりやすくするために、Constraintクラスの定義を追加しました。 –