C# 文本框验证将按钮边框设置为红色
我有一个C# 文本框验证将按钮边框设置为红色,c#,wpf,xaml,C#,Wpf,Xaml,我有一个ItemsControl,在这里我动态创建一个新的GroupBox,其中包含多个控件和模型。到目前为止,这是可行的。我还对我的TextBoxes实现了验证,它也按预期工作。还有一个按钮来删除这个组框,它绑定到用户控件类型的祖先 <ItemsControl Grid.Row="2" ItemsSource="{Binding StorageLocationList, Mode=TwoWay}"> <ItemsControl.ItemTemplate>
ItemsControl
,在这里我动态创建一个新的GroupBox
,其中包含多个控件和模型。到目前为止,这是可行的。我还对我的TextBox
es实现了验证,它也按预期工作。还有一个按钮
来删除这个组框
,它绑定到用户控件
类型的祖先
<ItemsControl Grid.Row="2" ItemsSource="{Binding StorageLocationList, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<GroupBox Style="{StaticResource GroupBoxBase}">
<GroupBox.Header>
<CheckBox x:Name="ExportGroupCheckBox" Content="Storage Location active" IsChecked="{Binding GroupActive, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource CheckBoxBase}" IsEnabled="{Binding ElementName=ActivateExportCheckBox, Path=IsChecked}"/>
</GroupBox.Header>
<Grid>
<Grid IsEnabled="{Binding ElementName=ExportGroupCheckBox, Path=IsChecked}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Name:" VerticalAlignment="Center"/>
<TextBox Grid.Row="0" Grid.Column="1" Style="{StaticResource LimitedCharTextBox}" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/>
<Label Grid.Row="1" Grid.Column="0" Content="Storage Location:" VerticalAlignment="Center"/>
<TextBox Grid.Row="1" Grid.Column="1" IsReadOnly="True" Style="{StaticResource BaseTextBox}" Text="{Binding StorageLocationPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"/>
<Button Grid.Row="1" Grid.Column="2" Content="Browse..." VerticalAlignment="Stretch" Command="{Binding StorageLocationBrowseCommand}" Style="{StaticResource ButtonBase}"/>
</Grid>
<Canvas>
<Button Canvas.Top="0" Canvas.Right="0" Content="X" ToolTip="Remove Group" Style="{StaticResource RemoveButton}" Command="{Binding ElementName=GPUserControl, Path=DataContext.RemoveStorageLocationGroupCommand}" CommandParameter="{Binding}"/>
</Canvas>
</Grid>
</GroupBox>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
但这也会将文本框Validation.ErrorTemplate
设置为null
那么它们是如何相互联系的呢?通过用户控件
以下是它的屏幕截图:
编辑:
以下是样式
:
<Style x:Key="BaseTextBox" TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextWrapping" Value="NoWrap"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="Background" Value="LightGray"/>
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="GroupBoxBase" TargetType="GroupBox">
<Setter Property="Padding" Value="2"/>
</Style>
<Style x:Key="ConfigurationMainWindownButton" TargetType="Button">
<Setter Property="Height" Value="Auto"/>
<Setter Property="Width" Value="70"/>
<Setter Property="Margin" Value="2,2,2,2"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
<Style x:Key="RemoveButton" TargetType="Button" BasedOn="{StaticResource ConfigurationMainWindownButton}">
<Setter Property="VerticalAlignment" Value="Top"/>
<Setter Property="HorizontalAlignment" Value="Right"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="Width" Value="20"/>
<Setter Property="Height" Value="20"/>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="Bold"/>
<!-- <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/> -->
</Style>
WPF将所有ItemsControl标记为无效,不仅是因为使用了相同的数据模型(实际上模型无效-不是控件)而将您的文本框标记为无效。文本框刚将状态设置为无效值。如果按钮必须忽略模型的验证状态,则可以通过覆盖Remove Group button控件的Validation.ErrorTemplate样式设置来解决此问题
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
WPF将所有ItemsControl标记为无效,不仅是因为使用了相同的数据模型(实际上模型无效-不是控件)而将您的文本框标记为无效。文本框刚将状态设置为无效值。如果按钮必须忽略模型的验证状态,则可以通过覆盖Remove Group button控件的Validation.ErrorTemplate样式设置来解决此问题
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder />
</ControlTemplate>
</Setter.Value>
</Setter>
请提供一份包含您的样式的表格。@mm8这是否足以作为一份合适的表格?它设置为RemoveButton样式。它可能与验证无关state@OlegBondarenko前台
正在设置按钮的文本颜色
。请提供一个包含您的样式的文本颜色。@mm8这是否足够设置正确的颜色?它设置为RemoveButton样式。它可能与验证无关state@OlegBondarenko前台
正在设置按钮的文本颜色
。