我有一个WPF DataGrid模板列,它具有来自wpf工具包的AutoCompleteBox的DataTemplate。在RowEditEnding事件和验证过程中,我无法在模板列中看到内容。
<DataGridTemplateColumn Header="Account Type" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<toolkit:AutoCompleteBox Text="{Binding Path='Account Type'}" Populating="PopulateAccountTypesACB" IsTextCompletionEnabled="True" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if ((value as BindingGroup).Items.Count == 0)
return new ValidationResult(true, null);
DataRowView row = (value as BindingGroup).Items[0] as DataRowView;
if (row != null)
{
if (ValidateAccountName(row.Row.ItemArray[0].ToString()))
{
return new ValidationResult(true, null);
}
else
{
return new ValidationResult(false,
"Account Name must be between 1 and 100 Characters.");
}
}
else
return new ValidationResult(true, null);
}
当我在创建DataRowView后在验证函数中放置断点时,模板列为空。我如何获得其内容?
答案 0 :(得分:0)
首先,您在AutoCompleteBox.Text属性的绑定路径中有一个空格,我认为不允许这样做。
答案 1 :(得分:0)
在研究之后,看起来它与DataGridTemplateColumn没有任何关系,而是与Wpf Toolkit中的AutoCompleteBox有关。自从我开始使用它以来,AutoCompleteBox对我来说一直是麻烦。结果,我决定废弃它并使用可编辑的ComboBox代替。组合框更清洁,更易于实施。以下是我的代码现在的样子,数据视图能够看到用户在框中输入的内容:
<DataGridTemplateColumn Header="Account Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path='Account Type'}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox IsEditable="True" LostFocus="LostFocusAccountTypes" ItemsSource="{DynamicResource types}" Height="23" IsTextSearchEnabled="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Code Behind(this.Types是一个可观察的字符串集合)
private void PopulateAccountTypes()
{
try
{
string accountQuery = "SELECT AccountType FROM AccountType WHERE UserID = " + MyAccountant.DbProperties.currentUserID + "";
SqlDataReader accountType = null;
SqlCommand query = new SqlCommand(accountQuery, MyAccountant.DbProperties.dbConnection);
accountType = query.ExecuteReader();
while (accountType.Read())
{
this.Types.Add(accountType["AccountType"].ToString());
}
accountType.Close();
Resources["types"] = this.Types;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}