0

ここにはこれと同様の質問がありますが、言及された解決策を試してみましたが役に立ちませんでした!

セットアップを実行させてください - IDataErrorInfo を実装するモデル、モデルをビューに公開するビューモデルがあります。ビュー内には、単にラベルの付いたテキスト ボックスであるユーザー コントロールがあり、モデル プロパティはユーザー コントロールの内部にバインドされています。依存関係プロパティを介したテキストボックス...そしてすべてが正しくバインドされ、すべての検証が開始され、正しいエラーが返されます! ただし、ユーザーコントロールがエラーを傍受しているように見えるため、テキストボックスではなくユーザーコントロールのエラーテンプレートが表示されます。

したがって、プロパティを x:Null に設定することでユーザー コントロールのエラー テンプレートが表示されないようにできることはわかっていますが、テキスト ボックスのエラー テンプレートを表示するにはどうすればよいでしょうか?! ユーザーコントロール内にIDataErrorInfoを実装し(一部のアドバイスに従って)、ユーザーコントロール内で検証エラーテンプレートを明示的に定義しようとしましたが、表示するものを取得できません。この時点で、ユーザーコントロールは単にエラーを傍受し、エラーを保持してテキストボックスに渡さないと考えています。したがって、エラーを認識していないため、エラーテンプレートは表示されません。

私は過去1日間髪を引っ張っていましたが、これを達成できることはわかっているので、ユーザーコントロールを使用しないことに頼りたくありませんが、それを修正する方法が本当にわかりません! ですから、助けてくれる魔法使いがそこにいるなら、私はとても感謝しています!

ユーザー コントロール XAML:

<UserControl x:Class="PIRS_Client.Control.LabelTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="40.541" Width="321.027">
<Grid Height="41" VerticalAlignment="Top" HorizontalAlignment="Left" Width="321">        
    <StackPanel Orientation="Horizontal" Margin="0,8,50,9">
    <Label Content="Label" Height="28" Name="BaseLabel" VerticalAlignment="Top" HorizontalContentAlignment="Right" Width="116" FontSize="11" />
        <TextBox Height="22" Width="100" Margin="0,0,0,0" x:Name="BaseTextBox" VerticalContentAlignment="Center" VerticalAlignment="Top" FontSize="11"/>
    </StackPanel>
</Grid>

ユーザー コントロール コード:

public partial class LabelTextBox : UserControl
{

    public static readonly DependencyProperty TextBoxTextProperty = DependencyProperty.Register("TextBoxText", typeof(string), typeof(LabelTextBox), new FrameworkPropertyMetadata() { BindsTwoWayByDefault = true });

    public LabelTextBox()
    {
        InitializeComponent();

        Binding textBoxText = new Binding("TextBoxText") { Source = this, Mode = BindingMode.TwoWay };
        BaseTextBox.SetBinding(TextBox.TextProperty, textBoxText);
    }        

    [Browsable(true)]
    public string LabelText
    {
        get { return BaseLabel.Content.ToString(); }
        set
        {
            BaseLabel.Content = value;
        }
    }

    [Browsable(true)]
    public string TextBoxText
    {
        get { return (string)GetValue(TextBoxTextProperty); }
        set { SetValue(TextBoxTextProperty, value); }
    }

    [Browsable(true)]
    public double TextBoxWidth
    {
        get { return BaseTextBox.Width; }
        set
        {
            BaseTextBox.Width = value;
        }
    }
}

表示 - UserControl 宣言:

        <control:LabelTextBox HorizontalAlignment="Left" LabelText="Email" TextBoxText="{Binding UpdateSourceTrigger=LostFocus, Path=NewFosterCarerInfo.partner_email, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" TextBoxWidth="120" Margin="190,182,-61,0" VerticalAlignment="Top" Height="41" Width="321"/>
4

1 に答える 1

4

私の問題を抱えている人のために、ここに作業コードがあります

ユーザー コントロール xaml:

<UserControl x:Class="PIRS_Client.Control.LabelTextBox"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Height="40.541" Width="321.027"             
         x:Name="Parent" Validation.ErrorTemplate="{x:Null}">

<Grid Height="41" VerticalAlignment="Top" HorizontalAlignment="Left" Width="321" DataContext="{Binding ElementName=Parent, ValidatesOnDataErrors=True}">
    <StackPanel Orientation="Horizontal" Margin="0,8,50,9">
        <Label Content="Label" Height="28" Name="BaseLabel" VerticalAlignment="Top" HorizontalContentAlignment="Right" Width="116" FontSize="11" />
        <TextBox Height="22" Text="{Binding Path=TextBoxText, ValidatesOnDataErrors=True}" Width="100" Margin="0,0,0,0" x:Name="BaseTextBox" VerticalContentAlignment="Center" VerticalAlignment="Top" FontSize="11"/>
    </StackPanel>
</Grid>

UserControl コード ビハインド:

public partial class LabelTextBox : UserControl, IDataErrorInfo
{       
    public LabelTextBox()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextBoxTextProperty =
        DependencyProperty.Register(
            "TextBoxText",
            typeof(string),
            typeof(LabelTextBox),
            new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)
    );

    #region IDataErrorInfo Members

    public string Error
    {
        get
        {
            if (Validation.GetHasError(this))
                return string.Join(Environment.NewLine, Validation.GetErrors(this).Select(e => e.ErrorContent));

            return null;
        }
    }

    public string this[string columnName]
    {
        get
        {
            // use a specific validation or ask for UserControl Validation Error 
            if (Validation.GetHasError(this))
            {
                var error = Validation.GetErrors(this).FirstOrDefault(e => ((BindingExpression)e.BindingInError).TargetProperty.Name == columnName);
                if (error != null)
                    return error.ErrorContent as string;
            }

            return null;
        }
    }

    #endregion

    [Browsable(true)]
    public string LabelText
    {
        get { return BaseLabel.Content.ToString(); }
        set { BaseLabel.Content = value; }
    }

    [Browsable(true)]
    public string TextBoxText
    {
        get { return (string)GetValue(TextBoxTextProperty); }
        set { 
            SetValue(TextBoxTextProperty, value);
        }
    }

    [Browsable(true)]
    public double TextBoxWidth
    {
        get { return BaseTextBox.Width; }
        set { BaseTextBox.Width = value; }
    }
}

ユーザー コントロールの使用:

            <control:LabelTextBox HorizontalAlignment="Left" LabelText="Email" TextBoxText="{Binding Path=NewFosterCarerInfo.partner_email, ValidatesOnDataErrors=true}" TextBoxWidth="120" Margin="190,182,-61,0" VerticalAlignment="Top" Height="41" Width="321"/>

そして、素敵な Validation.ErrorTemplate が必要な場合:

`<Style TargetType="{x:Type TextBox}">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Margin" Value="0,2,40,2" />
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="true">
                    <Border Background="Red" DockPanel.Dock="right" Margin="5,0,0,0" Width="20" Height="20" CornerRadius="10"
                                ToolTip="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                        <TextBlock Text="!" VerticalAlignment="center" HorizontalAlignment="center" FontWeight="Bold" Foreground="white">
                        </TextBlock>
                    </Border>
                    <AdornedElementPlaceholder Name="customAdorner" VerticalAlignment="Center" >
                        <Border BorderBrush="red" BorderThickness="1" />
                    </AdornedElementPlaceholder>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>`
于 2013-09-16T09:44:30.380 に答える