开发者

Validation (with custom ErrorTemplate) for a DataTemplate

I can't figure this guy out / in desperate need of assistance.

I have an ItemsControl and a DataTemplate as the ItemTemplate

i.e.

<DataTemplate>
    <StackPanel>
       <TextBox Text={Binding Prop1}/>
       <TextBox Text={Binding Prop2}/>
    </StackPanel>
</DataTemplate>

I have seen plenty of examples for applying validation to the target of an individual binding (i.e. just validating each textbox separately). I'd like to have a visual change to the whole DataTemplate if there is ANYTHING wrong (either Prop1 OR Prop2), using an <AdornedelementPlaceholder/>, as I've s开发者_StackOverflow中文版een in many examples.

Essenetially, is there a way to show an error if ANY item in the object that represents my DataContext of the DataTemplate has a problem?


You could define custom ErrorTemplates to both bindings, which define the StackPanel as Validation.ValidationAdornerSite. This way the StackPanel can be adorned instead of the textboxes, whenever an error is detected.

See this link for more details: Vincent Sibal about validation with ValidationAdornerSite


Try setting a BindingGroup on a StackPanel like below and set individual validation rules on each of your TextBoxes. I haven't actually tried this but it should work.

<StackPanel>
    <StackPanel.BindingGroup>
         <BindingGroup />
    </StackPanel.BindingGroup>

    <TextBox Text={Binding Prop1}/>
    <TextBox Text={Binding Prop2}/>
</StackPanel>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜