我得到XamlParseException
,我不知道如何解决它。我希望能够控制输入。
XAML
<DataGridTemplateColumn Header="Naam" CellTemplate="{StaticResource ContactNameTemplate}" CellEditingTemplate="{StaticResource EditingContactNameTemplate}"/>
字典
<DataTemplate x:Key="ContactNameTemplate" >
<TextBox HorizontalAlignment="Center" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Name" Source="{Binding Name}" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<c:Text Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
<DataTemplate x:Key="EditingContactNameTemplate">
<TextBox HorizontalAlignment="Center" Validation.ErrorTemplate="{StaticResource validationTemplate}" Style="{StaticResource textBoxInError}">
<TextBox.Text>
<Binding Path="Name" Source="{Binding Name}" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<c:Text Min="21" Max="130"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</DataTemplate>
修改
我的错误
PresentationFramework.dll中出现'System.Windows.Markup.XamlParseException'类型的第一次机会异常
附加信息:无法在Binding类型的Source属性中设置Binding。绑定只能在DependencyObject的DependencyProperty中设置。
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<TextBlock Foreground="Red" FontSize="20">!</TextBlock>
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
public class Text : ValidationRule
{
private int _min;
private int _max;
public Text()
{
}
public int Min
{
get { return _min; }
set { _min = value; }
}
public int Max
{
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
ValidationResult result;
string text;
if (((string)value).Length < Min || ((string)value).Length > Max)
{
text = value.ToString();
result = new ValidationResult(true, null);
}
else
{
text = "";
result = new ValidationResult(false, "De naam moet tss " + Min + " en " + Max);
}
return result;
}
}