2010-10-10 10 views
6

dodaję reguły walidacji do tekstowym (poniższe przykłady w WPF 4 Unleashed Adam Nathan)zapewnić wartość na „System.Windows.Data.Binding” zwrócił wyjątek

Po uruchomieniu aplikacji, jestem do czynienia z po tajemniczym komunikatem o błędzie

podać wartość na 'System.Windows.Data.Binding' zwrócił wyjątek

alt text

Co oznacza błąd i co należy zrobić, aby rozwiązać problem?

Oto pełny kod źródłowy

XAML

<Window.Resources> 
    <Style x:Key="controlStyle" TargetType="{x:Type Control}"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="RenderTransform"> 
        <Setter.Value> 
         <RotateTransform Angle="20" /> 
        </Setter.Value> 
       </Setter> 
       <Setter Property="Foreground" Value="Black" /> 
      </Trigger> 
     </Style.Triggers> 
     <Setter Property="FontSize" Value="22" /> 
     <Setter Property="Background" Value="Purple" /> 
     <Setter Property="Foreground" Value="White" /> 
     <Setter Property="Height" Value="50" /> 
     <Setter Property="Width" Value="50" /> 
     <Setter Property="RenderTransformOrigin" Value=".5,.5" /> 
     <Setter Property="RenderTransform"> 
      <Setter.Value> 
       <RotateTransform Angle="10" /> 
      </Setter.Value> 
     </Setter> 
     <Setter Property="TextBox.TextAlignment" Value="Right" /> 
    </Style> 
    <Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}"> 
     <Style.Triggers> 
      <Trigger Property="Validation.HasError" Value="True"> 
       <Setter Property="Background" Value="Red" /> 
       <Setter Property="ToolTip" 
         Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 

<StackPanel Orientation="Vertical"> 
    <StackPanel Orientation="Horizontal"> 
     <TextBox Style="{StaticResource textBoxStyle}"> 
      <TextBox.Text> 
       <Binding> 
        <Binding.ValidationRules> 
         <local:JpgValidationRule /> 
        </Binding.ValidationRules> 
       </Binding> 
      </TextBox.Text> 
     </TextBox> 
    </StackPanel> 

    <StackPanel Orientation="Horizontal"> 
     <Button Style="{StaticResource controlStyle}">1</Button> 
     <ComboBox Style="{StaticResource controlStyle}"> 
      <ComboBox.Items>2</ComboBox.Items> 
     </ComboBox> 
     <Expander Style="{StaticResource controlStyle}" Content="3" /> 
     <TabControl Style="{StaticResource controlStyle}"> 
      <TabControl.Items>4</TabControl.Items> 
     </TabControl> 
     <ToolBar Style="{StaticResource controlStyle}"> 
      <ToolBar.Items>5</ToolBar.Items> 
     </ToolBar> 
     <!--<InkCanvas Style="{StaticResource controlStyle}" />--> 
     <TextBox Style="{StaticResource controlStyle}" Text="7" /> 
    </StackPanel> 
</StackPanel> 

JpgValidationRule

using System.Globalization; 
using System.IO; 
using System.Windows.Controls; 

namespace StylesDemo 
{ 
    public class JpgValidationRule : ValidationRule 
    { 
     public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
     { 
      string fileName = value.ToString(); 

      // Reject nonexistent files: 
      if (!File.Exists(fileName)) 
       return new ValidationResult(false, "Value is not a valid file!"); 

      // Reject files that don't end in .jpg: 
      if (Path.GetExtension(fileName).ToUpper() != ".JPG") 
       return new ValidationResult(false, "Value is not a .jpg file!"); 

      return new ValidationResult(true, null); 
     } 
    } 
} 

Odpowiedz

5

Myślę, że jest on powiązany z domyślnym trybem bindowania dla TextBox.Text, który jest TwoWay, i dla tej ścieżki jest wymagany. Możesz spróbować ustawić Mode="OneWay" jako atrybut w tagu Binding, który powinien tutaj działać, ale nie jestem całkowicie pewien Twoich ogólnych wymagań.

Powiązane problemy