好奇是否有另一种方法来验证 ListView 中的所有 TextBox

发布于 2024-10-30 19:22:37 字数 3312 浏览 0 评论 0原文

这是我所拥有的......显示的是 ListView 的一部分。当前发生的情况是这样的:用户单击“新建”按钮来创建新行。在单击“保存”按钮之前,用户可以也可以不向包含的文本框/组合框输入信息。单击“保存”按钮时,将执行下面的 C# 代码,该代码使用 VisualTreeHelper 查找 ListView 中的所有文本框/组合框,并确保用户已输入文本/选择项目。如果没有,它会将 BorderBrush 设置为红色并阻止 ObjectContext 保存更改。我知道在某些情况下您可以使用 ValidationRule...但我不知道如何使其验证在单击“保存”按钮之前从未选择/更改的新行。这可行,但很好奇是否有更优雅的方法。

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

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

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文