0
votes

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?

2
Just to be clear, you want the value typed or selected by the user?slugster
I would like the value typed in by the user.Eric R.

2 Answers

0
votes

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.

0
votes

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