0

コントロールのグループの検証をテストするための良い解決策を見ました:

私の Window.xaml.cs で:

 private bool IsValid(DependencyObject obj)
    {
        // The dependency object is valid if it has no errors, 
        //and all of its children (that are dependency objects) are error-free.
        return !Validation.GetHasError(obj) &&
            LogicalTreeHelper.GetChildren(obj)
            .OfType<DependencyObject>()
            .All(child => IsValid(child));
    }

私の問題は、私の場合、エラーが発生した場合でも、isValid が常に true を返すことです。xamlのミスが原因だと思いますが...どこですか?

ここに私の Windows.XAML があります:

<Window.Resources>
    <CommandBinding x:Key="binding" Command="Save" Executed="Save_Executed" CanExecute="Save_CanExecute" />
</Window.Resources>

<TextBox Name="TextBox_TypeEvenement" Grid.Column="1" VerticalAlignment="Center" Height="20">
  <TextBox.Text>
    <Binding Path="strEvtType">
        <Binding.ValidationRules>
            <ExceptionValidationRule />
        </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

<Button Template="{StaticResource BoutonRessourcesTpl}" Command="Save">
  <Button.CommandBindings>
    <StaticResource ResourceKey="binding"></StaticResource>
  </Button.CommandBindings>
  <Image Source= "Toolbar_Valider.png" Height="16"/>
</Button>

そして、ここに私の Class.cs c# があります:

private string m_strEvtType;
public string strEvtType
{
get { return m_strEvtType; }
set {
        m_strEvtType = value;
        if (m_objEvtCode.ReadEvtTypebyType(m_strEvtType) != 0)
        {
            throw new ApplicationException(m_strEvtType.Trim() + " est innexistant !");
        }
        FirePropertyChangedEvent("strEvtType");
        FirePropertyChangedEvent("m_objEvtCode.strDes");
    }
}

isValid が常に true を返す理由がわかりましたか?

どうもありがとう :)

よろしくお願いします :)

4

1 に答える 1

0

All問題はあなたの使い方だと思いますLinq

ソース シーケンスのすべての要素が指定された述語のテストに合格した場合、またはシーケンスが空の場合、 All は true を返します。それ以外の場合は false。

DependencyObjectsそのため、 will に子がない場合はDependencyObject Alltrue を返します

状態Anyを確認するなど、別のLinqメソッドを使用してみてくださいIsValid

 return !Validation.GetHasError(obj) &&
            !LogicalTreeHelper.GetChildren(obj)
            .OfType<DependencyObject>()
            .Any(child => !IsValid(child));
于 2013-03-20T23:08:45.200 に答える