WPF对子控件的验证

时间:2015-06-25 17:50:13

标签: c# wpf

我有一个带有TextBox的UserControl,如下所示:

<UserControl x:Class="xxx.CommonControls.Views.InPlaceEdit"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:catel="http://catel.codeplex.com"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Name="parent"
             mc:Ignorable="d">

    <Border Background="Transparent" MouseLeftButtonDown="UIElement_OnMouseLeftButtonDown">
        <TextBox x:Name="ButtonEdit"
                Text="{Binding ElementName=parent,
                               Path=Value,
                               Mode=TwoWay,
                               UpdateSourceTrigger=PropertyChanged}">
        </TextBox >
    </Border>

</UserControl>

用法:

<controls:InPlaceEdit Grid.Row="0"
    Grid.Column="1"
    Height="25"
    Margin="5,0,0,5"
    Value="{Binding SelectedPatient.Name, UpdateSourceTrigger=PropertyChanged}" />

问题是:如何将验证错误移动到我的TextBox?这是因为所有ValidationErrors都存储在我的InPlaceEdit控件中,而没有正确填充到TextBox。

(这是简化的视图,但基本上显示了我的问题。在我的应用程序中,我使用DevExpress的ButtonEdit而不是TextBox)

1 个答案:

答案 0 :(得分:0)

我是通过从子控件中删除Text绑定并将绑定设置为与父控件完全相同来完成的:

this.Loaded += (sender, args) =>
{
    var binding = BindingOperations.GetBinding(this, ValueProperty);
    if (binding != null)
    {
        BindingOperations.SetBinding(ButtonEdit, DevExpress.Xpf.Editors.TextEditBase.TextProperty, binding);
    }
};