我没有收到任何绑定错误,并且此代码在另一个地方工作。我还没有发现我现在所做的与它工作的代码有什么不同,而且代码不多。
在UserControl.Resource 中:
<Style TargetType="TextBox">
  <Setter Property="BorderBrush" Value="DarkBlue"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="Margin" Value="0,1,0,1"/>
  <Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
      <ControlTemplate>
        <StackPanel Orientation="Horizontal">
          <AdornedElementPlaceholder/>
          <Grid Margin="2,0,0,0">
            <Ellipse Width="20" Height="20" Fill="Red"/>
            <TextBlock Foreground="White" Text="X" FontWeight="Bold"
                       HorizontalAlignment="Center" VerticalAlignment="Center"/>
          </Grid>
        </StackPanel>
      </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>
在 Xaml 下面也是:
<TextBlock Height="23" HorizontalAlignment="Left" Margin="22,90,0,0"
           Text="Keywords" VerticalAlignment="Top"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="22,108,0,0"
         VerticalAlignment="Top" Width="244">
  <Binding Path="Tags" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
    <Binding.ValidationRules>
      <DataErrorValidationRule ValidatesOnTargetUpdated="False"/>
    </Binding.ValidationRules>
  </Binding>
</TextBox>
Model.Tags我的 ViewModel 中的按钮 SAVE 仅在属性超过用户输入的 10 个字符时才被激活。当我输入 10,11 然后返回 8 个字符时,按钮激活/禁用工作正常。所有属性更改都被触发。
型号:
namespace TBM.Model
{
    public class Document : EntityBase , IDataErrorInfo
    {
        public int Id { get; set; }
        public string DocumentName { get; set; }
        public string Tags { get; set; }
        public byte[] DocumentData { get; set; }
        public int PeriodId { get; set; }
        string IDataErrorInfo.Error { get { return null; } }
        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;
                return true;
            }
        }
        static readonly string[] ValidatedProperties = { "Tags", };
        private string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;
            string error = null;
            switch (propertyName)
            {               
                case "Tags": error = this.IsTagsEmpty(Tags); break;
                default:
                    Debug.Fail("Unexpected property being validated on Document: " + propertyName);
                    break;
            }
            return error;
        }  
        private string IsTagsEmpty(string value)
        {
            if (value != null && value.Trim().Length >= 10)
                return null;
            else
               return "The keywords must have at least 10 chars!";            
        }
    }
}
视图模型:
 public RelayCommand SaveDocumentCommand
 {
     get { return _saveDocumentCommand ?? (_saveDocumentCommand =
         new RelayCommand(() => SaveDocument(),() => CanSaveDocument())); }
     }
     private bool CanSaveDocument()
     {
         return _document.IsValid;
     }
//...
什么不起作用是带有红色椭圆的 ErrorTemplate 根本没有显示?
更新:下面的代码完全适用于 TEST 项目。但是在我的生产项目中它没有找到资源???为什么会这样?
<TextBlock Height="23" HorizontalAlignment="Left" Margin="22,89,0,0"
           Text="Keywords" VerticalAlignment="Top"/>
  <TextBox Style="{StaticResource bla}" Height="23" HorizontalAlignment="Left"
           Margin="22,109,0,0" VerticalAlignment="Top" Width="244">
    <Binding Path="Tags" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <DataErrorValidationRule ValidatesOnTargetUpdated="False"
                                 ValidationStep="UpdatedValue"/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox>
<UserControl.Resources>
  <Style x:Name="bla" TargetType="TextBox">
    <Setter Property="BorderBrush" Value="DarkBlue"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Margin" Value="0,1,0,1"/>
    <Setter Property="Validation.ErrorTemplate">
      <Setter.Value>
        <ControlTemplate>
          <StackPanel Orientation="Horizontal">                          
            <AdornedElementPlaceholder/>
            <Grid Margin="2,0,0,0">
              <Ellipse Width="20" Height="20" Fill="Red"/>
              <TextBlock Foreground="White" Text="X" FontWeight="Bold"
                         HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Grid>
          </StackPanel>
        </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>
</UserControl.Resources>