好奇是否有另一种方法来验证 ListView 中的所有 TextBox
这是我所拥有的......显示的是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论