3

Here is what i have....shown is part of the ListView. What currently happens is this: User clicks on New button to create a new row. User may or may not enter info into the contained TextBoxes/Comboboxes prior to clicking the Save button. When the Save button is clicked the C# code below is executed which uses the VisualTreeHelper to find all TextBoxes/ComboBoxes in the ListView and make sure the user has entered text/selected an item. If they have not it sets the BorderBrush red and prevents the ObjectContext from saving changes. I know that you can use a ValidationRule in some cases...but i couldn't figure out how to make it validate a new row that was never selected/changed prior to clicking the Save button. This works but was curious if there was a more elegant way.

XAML

 <ListView.View>
            <GridView>
                <GridViewColumn x:Name="nameColumn" Header="Name" Width="100">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <TextBox Margin="-6,-1" >
                                <TextBox.Text>
                                    <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                                        <Binding.ValidationRules>
                                            <me:TextInputValidator />
                                        </Binding.ValidationRules>
                                    </Binding>
                                </TextBox.Text>
                            </TextBox>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

C#

 Stack<FrameworkElement> tree = new Stack<FrameworkElement>();
            tree.Push(subcategoryListView);

            while (tree.Count > 0)
            {
                FrameworkElement current = tree.Pop();

                int count = VisualTreeHelper.GetChildrenCount(current);
                for (int i = 0; i < count; ++i)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(current, i);
                    if (child is FrameworkElement)
                        tree.Push((FrameworkElement)child);
                    if (child is TextBox)
                    {
                        TextBox tempBox = child as TextBox;
                        if (string.IsNullOrEmpty(tempBox.Text))
                        {
                            tempBox.BorderBrush = new SolidColorBrush(Colors.Red);
                            errorsOnPage = true;
                        }

                    }
                    if (child is ComboBox)
                    {
                        ComboBox tempCombo = child as ComboBox;
                        if (tempCombo.SelectedIndex == -1)
                        {
                            tempCombo.BorderBrush = new SolidColorBrush(Colors.Red);
                            errorsOnPage = true;
                        }
                    }
                }
            }

            if (!errorsOnPage)
            {
                dbcontext.SaveChanges();
                MessageBox.Show("Saved", null, MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                MessageBox.Show("Highlighted fields are missing or invalid.", null, MessageBoxButton.OK, MessageBoxImage.Stop);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error encountered while saving", null, MessageBoxButton.OK, MessageBoxImage.Stop);
        }

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.