访问 DataGridTemplateColumn 内容

发布于 2024-12-21 00:55:40 字数 1342 浏览 5 评论 0原文

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

I have a WPF DataGrid templatecolumn that has a DataTemplate for an AutoCompleteBox from the wpf toolkit. During RowEditEnding event and validation procedures, I am not able to see the content in the templatecolumn.

<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);
    }

When I put a break point in my validation function after I create the DataRowView, the template column is empty. How would I get its content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨曦÷微暖 2024-12-28 00:55:40

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

For a start you have a space in the Path of your Binding for the AutoCompleteBox.Text property which I don't think is allowed.

千笙结 2024-12-28 00:55:40

经过研究后,它似乎与 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);
        }
    }

After looking into this, it seems like it doesn't have anything to do with the DataGridTemplateColumn, but rather with the AutoCompleteBox from the Wpf Toolkit. The AutoCompleteBox has been nothing but trouble for me ever since I started using it. As a result, I decided to scrap it and use an Editable ComboBox instead. The combobox is much cleaner and more simple to implement. Here is how my code now looks and the datarowview is able to see what the user types in the box:

<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 is an observable collection of strings)

    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);
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文