Xaml代码:
<StackPanel Grid.Row="1" Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" >
<TextBox Style="{StaticResource EditValueStyle}" HorizontalAlignment="Left" Text="{Binding TemperatureManualInput,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=true, NotifyOnValidationError=true }" Height="Auto" VerticalAlignment="Center" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" />
<TextBlock Text="{Binding Units}" Style="{StaticResource _unit_rangeStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" />
<TextBlock Text="{Binding ManualTemperatureInputRange}" Style="{StaticResource _unit_rangeStyle}" HorizontalAlignment="Left" VerticalAlignment="Center" />
</StackPanel>
Style.xaml
<Style x:Key="EditValueStyle" TargetType="TextBox">
<Setter Property="Control.HorizontalAlignment" Value="Left" />
<Setter Property="Control.VerticalAlignment" Value="Center" />
<Setter Property="Control.Height" Value="Auto" />
<Setter Property="Control.HorizontalContentAlignment" Value="Right" />
<Setter Property="Control.VerticalContentAlignment" Value="Center" />
<Setter Property="Control.Foreground" Value="#333" />
<Setter Property="Control.Width" Value="60" />
<Setter Property="Control.FontSize" Value="14" />
<Setter Property="MaxLength" Value="8"></Setter>
<Setter Property="Control.Margin" Value="0,0,15,0" />
<Setter Property="Control.FontFamily" Value="Segoe UI Symbol" />
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="true">
<Border Background="Red" DockPanel.Dock="right" Margin="3,0,0,0" Width="15" Height="15" CornerRadius="10" ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
</TextBlock>
</Border>
<AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
<Border BorderBrush="red" BorderThickness="1" />
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
的.cs
public float TemperatureManualInput
{
get { return _temperatureManualInput; }
set
{
_temperatureManualInput = value;
OnPropertyChanged(nameof(TemperatureManualInput));
EnableDisableSave?.Invoke();
// DisableSaveButton?.Invoke(false);
}
}
public string Error
{
get { return String.Empty; }
}
public string this[string columnName]
{
get
{
String errorMessage = String.Empty;
switch (columnName)
{
case "TemperatureManualInput":
if (TemperatureManualInput < ManualTemperatureInputMin || TemperatureManualInput > ManualTemperatureInputMax)//ExternalTemperatureInputMax< TwentyMATemperatureValue || ExternalTemperatureInputMin> TwentyMATemperatureValue)
{
errorMessage = "Out of Range";
// SaveButtonStatusChanged();
IsEnabled = false;
//DisableSaveButton(false);
}
break;
}
if (errorMessage != string.Empty && !(Errors.ContainsKey(columnName)))Errors.Add(columnName, errorMessage);
return errorMessage;
}
}
这是我的简短代码,我有一个允许浮点的文本框,但在添加验证后它不允许浮动。请帮我解决。谢谢提前。