开发者

WPF ContentControl ErrorTemplate

开发者 https://www.devze.com 2023-03-25 12:39 出处:网络
I have a content control that has 2 data templates as follows; <ContentControl Content=\"{Binding ContentViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\">

I have a content control that has 2 data templates as follows;

<ContentControl Content="{Binding ContentViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">                    
<ContentControl.Resources>
<DataTemplate DataType="{x:Type vm:TypeA}">
<vs:TypeAView  />
</DataTemplate>                            

<DataTemplate DataType="{x:Type vm:TypeB}">
<vs:TypeBView  />                                
</DataTemplate>
</ContentControl.Resources>
</ContentControl>

My problem is that both TypeA view and TypeB view have a textbox that is bound to a Name property. On my containing control, when an item is selected from a treeview the ContentViewModel property is set.

The data displays and can be edited as I want. However I have a problem when my TypeB view's name field is made invalid (I'm using EntLib 5 validation and have implemented IDataErrorInfo - so my binding has ValidatesOnDataErrors=True). Whilst the error template for the control is displayed as expected, when I then click on to TypeA in the treeview the error message is still displayed even though TypeA's name textbox is valid.

It's as if the binding is getting mixed up which is which. Strange thing is that it only happens one way, i.e. going from an invalid TypeB name to the TypeA view. Doesn't seem to happen when going from an invalid TypeA view.

Can anyone help indicate what I've done wrong?

This is the error template I'm using for the textbox;

<Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="true">
                            <Border DockPanel.Dock="Left" Margin="0,0,2,0" Width="14" Height="14" CornerRadius="10"
                                ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors), Converter={StaticResource ErrorContentConverter}}">
                                <Border.Background>
                                    <RadialGradientBrush GradientOrigin="0.2,0.2" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
                                        <RadialGradientBrush.GradientStops>
                                            <GradientStop Color="Pink" Offset="0" />
                                            <GradientStop Color="Red" Offset="1" />
                                        </RadialGradientBrush.GradientStops>
                                    </RadialGradientBrush>
                 开发者_开发问答               </Border.Background>

                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Foreground="White" />
                            </Border>
                            <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center">
                                <Border BorderBrush="Red" BorderThickness="1" />
                            </AdornedElementPlaceholder>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
0

精彩评论

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