【问题标题】:Access DataGridTemplateColumn content访问 DataGridTemplateColumn 内容
【发布时间】:2011-12-13 02:09:23
【问题描述】:

我有一个 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 后在我的验证函数中放置一个断点时,模板列是空的。我如何获得它的内容?

【问题讨论】:

  • 明确一点,您希望用户输入或选择的值?
  • 我想要用户输入的值。

标签: c# wpf datagrid datagridtemplatecolumn


【解决方案1】:

首先,您在 AutoCompleteBox.Text 属性的绑定路径中有一个空格,我认为这是不允许的。

【讨论】:

  • 当我从数据库加载行时,我正在获取此列的值以显示我拥有的绑定。我认为它有效,因为我正在使用 SqlCommand 加载行,并且我返回的列的名称拼写相同。
【解决方案2】:

经过调查,似乎它与 DataGridTemplateColumn 没有任何关系,而是与 Wpf Toolkit 中的 AutoCompleteBox 有任何关系。自从我开始使用 AutoCompleteBox 以来,它对我来说一直是个麻烦。结果,我决定放弃它并改用可编辑组合框。组合框更干净,更易于实现。以下是我的代码现在的样子,并且 datarowview 能够看到用户在框中输入的内容:

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

代码背后(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);
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多