首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

WPF UserControl的ValidationRule触发了, 但是这个TextBox的默认的ErrorTemplate没有看到红框解决思路

2012-03-15 
WPF UserControl的ValidationRule触发了, 但是这个TextBox的默认的ErrorTemplate没有看到红框我创建了一个

WPF UserControl的ValidationRule触发了, 但是这个TextBox的默认的ErrorTemplate没有看到红框
我创建了一个UserControl工程,里面有个TextBox,绑定了一个ValidationRule, 然后我在另一个wpf窗体程序中插入了这个UserControl,调试发现Textbox的ValidationRule触发了,但是默认的红框没有出现。

如果我把UserControl基类改为Window, 单独ShowDialog, 红框是有的。

相关代码:

XML code
<UserControl    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    mc:Ignorable="d"    x:Class="UserControlValidationDemo.UserControl1"    xmlns:UserControlValidationDemo_ValidationRules="clr-namespace:UserControlValidationDemo.ValidationRules"    xmlns:UserControlValidationDemo_ViewModels="clr-namespace:UserControlValidationDemo.ViewModels"    x:Name="UserControl"    d:DesignWidth="640" d:DesignHeight="480">    <UserControl.Resources>        <UserControlValidationDemo_ViewModels:MainViewModel x:Key="MainViewModelDataSource" d:IsDataSource="True"/>    </UserControl.Resources>    <UserControl.DataContext>        <Binding Mode="OneWay" Source="{StaticResource MainViewModelDataSource}"/>    </UserControl.DataContext>        <Grid x:Name="LayoutRoot" Background="White">        <TextBox HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" x:Name="txtTest">            <TextBox.Text>                <Binding Path="Collection[0].Property1" UpdateSourceTrigger="PropertyChanged">                    <Binding.ValidationRules>                        <UserControlValidationDemo_ValidationRules:TestValidationRule />                    </Binding.ValidationRules>                </Binding>            </TextBox.Text>        </TextBox>    </Grid></UserControl>


namespace UserControlValidationDemo.ValidationRules
{
  public class TestValidationRule : ValidationRule
  {
  public override ValidationResult Validate(object value, CultureInfo cultureInfo)
  {
  if (GlobalDefinition.MainWnd is UserControl)
  {
  object t = Validation.GetErrorTemplate((GlobalDefinition.MainWnd as UserControl1).txtTest);
  object e = Validation.GetErrors((GlobalDefinition.MainWnd as UserControl1).txtTest);

  return new ValidationResult(false, "This is failed validation.");
  }
  else if (GlobalDefinition.MainWnd is Window)
  {
  object t = Validation.GetErrorTemplate((GlobalDefinition.MainWnd as Window1).txtTest);
  object e = Validation.GetErrors((GlobalDefinition.MainWnd as Window1).txtTest);

  return new ValidationResult(false, "This is failed validation.");
  }

  return new ValidationResult(false, "This is failed validation.");
  }
  }
}

C# code


[解决办法]
过来看看,顺便蹭点主任的1800
[解决办法]
我过来看看那些朋友会来帮忙

热点排行