바인딩에 문제가 있습니다. RelativeSource
가 여행을 원하는 조상을 찾을 수있는 시각적 트리를 필요로하기 때문에, 당신은 단지 UIElement
에서 사용할 수 있습니다하지만 난 RelativeSource
이 비 UIElement에 구속력을하려고 오전 ValidationRule입니다 같은되는 등 모두 VisualTree
도 아니고 UIElement
인지도 모릅니다. 당신은 구속력이 깨지는 것을 기대할 수 있습니다. RelativeSource
을 찾을 수 없습니다. 내가 말한 것처럼 VisualTree
또는 LogicalTree
을 사용할 수 없습니다. 나는 그것을 작동하게 할 필요가있다. 내가 DependencyObject에이 할 수 DepObjClass 전화에서 유도체 클래스를 만들 수 있었다 때문에비 UIElement에 바인딩
<StackPanel DataContext{Binding}>
<Grid>
<ContentControl Content{Binding MVPart1>
<TextBox>
<TextBox.Text>
<Binding Path="VMPart1Property1">
<Binding.ValidationRules>
<my:MyValidationRule>
<my:ValidationRule.DOC>
<my:DepObjClass DepProp={Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}}/>
</my:ValidationRule.DOC>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</ContentControl>
</Grid>
</StackPanel>
그래서 기본적으로 MyValidationRule이 ValidationRule 클래스에서 derivering하지만, UIElement에도 DependencyObject에하지 이잖아과 : 여기
는 XAML의 예입니다 xaml 바인딩 표현식을 써주세요.public class MyValidationRule : ValidationRule
{
public DepObjClass DOC
{
get;
set;
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string text = value as string;
if (!string.IsNullOrEmpty(text))
{
return new ValidationResult(true, string.Empty);
}
return new ValidationResult(false, "Not working blahhh");
}
}
public class DepObjClass : DependencyObject
{
public object DepProp
{
get
{
return (object)GetValue(DepPropProperty);
}
set
{
SetValue(DepPropProperty, value);
}
}
public static DependencyProperty DepPropProperty
= DependencyProperty.Register(typeof(object), typeof(DepObjClass)......);
}
지금 요약하기 : 여기
는 코드입니다. MyValidatonRule은 DependencyObject가 아닌 UIElement는 아니지만 형식의 속성을 가지므로 xaml 바인딩식이 컴파일됩니다.
나는 StackPanel의이 ValidationRule 나던 VisualTree을 가지고 있기 때문에도 내 유효성 검사 규칙은 논리 또는 Visual 트리에 참여 찾을 수 couldnt는 때문에 작업 자체를 밤은 바인딩 응용 프로그램을 실행할 때.
질문은, 어떻게 같은 내 ValidationRule 같은 비 UIElement에서 StackPanel에를 찾기 위해 나는 이러한 경우 작업을 어떻게입니까?
내 코드는 comipiling이 아니라고 사과하지만, 내가 뭘 하려는지 이해할 수 있기를 바랍니다. 저는 올바른 답을 얻기 위해 50 점을주고 있습니다.
[이] (같은 아마 뭔가 http://www.codeproject.com/Articles/27432/Artificial-Inheritance-Contexts-in-WPF) 도울 수있다. – dowhilefor
정확히 달성하려는 것은 무엇입니까?당신의 MyValidationRule 클래스는'DependencyObject' 타입의 프로퍼티를 가지고 있습니다 만 실제로 그것을 사용하지는 않습니다. –
그렇다면 dependeyobject는 dependencyproperty를 가지고 있고 xaml에서 바인딩을하고 있지만 바인딩 자체가 visualtree에 참여하지 않아 visualtree에서 작동하지 않기 때문에 바인딩이되지 않습니다. 어떻게 작동하게합니까? –