问题描述
我正在尝试在拖放过程中为放置目标制作一个装饰器,并且我正在创建TemplatedAdorner和AdornedElementPlaceholder类的自定义版本,以便这样做。我得到它的工作,除了我不能让装饰的
元素的内容显示在装饰者的背景前面,就像我可以使用TextBoxes ErrorTemplate。这是否意味着文本框中的文本实际上是在具有比验证装饰器更高的z顺序的装饰器中呈现的?
I am trying to make an adorner for drop targets during drag drop, and am creating custom versions of the TemplatedAdorner and AdornedElementPlaceholder classes in order to do so. I am getting it to work with the exception that I cannot get the adorned element's content to display in front of the adorner's background, like I can do with the TextBoxes ErrorTemplate. Does this mean that the text in a textbox is actually rendered in an adorner that has a higher z-order than the validation adorner?
推荐答案
是否要在errortemplate中显示错误消息,并提醒用户输入正确的值?如果是,请查看textBox的以下样式。
Do you want to display error message in errortemplate, and remind user to enter the correct value? If yes, please take a look the following style for textBox.
<Window.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<Grid>
<Border
x:Name="errorBorder"
Background="#11FF0000"
BorderBrush="#FFCB2E2E"
BorderThickness="1"
IsHitTestVisible="False" />
<AdornedElementPlaceholder x:Name="placeholder" />
<Popup
HorizontalAlignment="Right"
AllowsTransparency="True"
HorizontalOffset="0"
IsOpen="{Binding ElementName=placeholder, Path=AdornedElement.IsFocused, Mode=OneWay}"
Placement="Right"
PlacementTarget="{Binding ElementName=errorBorder}"
PopupAnimation="Fade"
VerticalOffset="0">
<StackPanel Orientation="Horizontal">
<Polygon
VerticalAlignment="Center"
Fill="#FFCB2E2E"
Points="0,4 4,0 4,8"
Stretch="Fill"
Stroke="#FFCB2E2E"
StrokeThickness="2" />
<Border
Padding="4"
Background="#FFCB2E2E"
CornerRadius="4">
<TextBlock
Margin="2,0,0,0"
HorizontalAlignment="Center"
FontWeight="Bold"
Foreground="White"
Text="{Binding ElementName=placeholder, Path=AdornedElement.ToolTip, Mode=OneWay}" />
</Border>
</StackPanel>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox
Name="textBox1"
Width="50"
Height="30">
<TextBox.Text>
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:AgeRangeRule Max="80" Min="20" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
.cs:
public partial class Window1 : Window
{
private MyDataSource ods { get; set; }
public Window1()
{
InitializeComponent();
ods = new MyDataSource();
ods.Age = 100;
this.DataContext = ods;
}
}
public class AgeRangeRule : ValidationRule
{
private int _min;
private int _max;
public AgeRangeRule()
{
}
public int Min
{
get { return _min; }
set { _min = value; }
}
public int Max
{
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
double enteredValue;
if (double.TryParse(value.ToString(), out enteredValue))
{
if ((enteredValue < Min) || (enteredValue > Max))
{
return new ValidationResult(false,
string.Format("Entered value must be in the range [{0};{1}].",
Min, Max));
}
}
else
{
return new ValidationResult(false,
string.Format("Value '{0}' is not of type double.", value));
}
return new ValidationResult(true, null);
}
}
public class MyDataSource: Test3.ViewModelBase
{
private int _age;
public MyDataSource()
{
Age = 0;
}
public int Age
{
get { return _age; }
set {
_age = value;
RaisePropertyChanged("Age");
}
}
}
最好的问候,
Cherry
这篇关于文本框如何在ErrorTemplate上方显示内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!