1

App.xaml、MainWindow.xaml、および Person クラスを使用した単純なアプリケーションがあります。テンプレートを指定しないと、ValidateOnDataErrors は完全に機能し、エラーが発生した場合はテキスト ボックスの周りに赤い境界線が表示されます。ただし、MainWindow.xaml の Window タグに「 Template="{StaticResource WindowTemplate}" 」を挿入するとすぐに、フィールドは引き続き検証されますが、赤い境界線は消えます。

App.xaml :

<Application x:Class="WpfPOC.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ControlTemplate TargetType="Window" x:Key="WindowTemplate">
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Control x:Name="FocusCatcher"></Control>
            <TextBlock>Menu Section</TextBlock>
            <ContentPresenter Grid.Row="1" />
            <StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
                <TextBlock Text="Current Editing Mode" />
            </StatusBar>
        </Grid>
    </ControlTemplate>
</Application.Resources>
</Application>

MainWindow.xaml :

<Window x:Class="WpfPOC.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:data="clr-namespace:WpfPOC"
    Template="{StaticResource WindowTemplate}"
    Title="MainWindow" 
    Height="350" 
    Width="525">
<Window.Resources>
    <data:Person x:Key="myDataSource" Name="Joe"/>
</Window.Resources>
<Grid>
    <TextBox Height="23" Width="120" Text="{Binding Source={StaticResource myDataSource}, Path=Name, ValidatesOnDataErrors=True}" />
</Grid>
</Window>

Person.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WpfPOC
{
public class Person : IDataErrorInfo
{
    public string Name { get; set; }

    #region IDataErrorInfo Members

    public string Error
    {
        get { return string.Empty; }
    }

    public string this[string columnName]
    {
        get { return "Simulated error"; }
    }

    #endregion
}
}

よろしくお願いいたします。

4

1 に答える 1

5

これは、検証エラーの表示に使用されるデフォルトのControlTemplate(コントロールの周囲に赤い境界線を描画する)がAdornerLayerを使用するために発生します。

ウィンドウ全体の新しいControlTemplateを作成し、AdornerDecorator(ウィンドウのデフォルトのControlTemplateが提供)を省略しました。

したがって、新しいControlTemplateを次のようにAdornerDecoratorでラップするだけです。

<ControlTemplate TargetType="Window" x:Key="WindowTemplate">
    <AdornerDecorator>
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Control x:Name="FocusCatcher"></Control>
            <TextBlock>Menu Section</TextBlock>
            <ContentPresenter Grid.Row="1" />
            <StatusBar Height="23" VerticalAlignment="Bottom" Grid.Row="2">
                <TextBlock Text="Current Editing Mode" />
            </StatusBar>
        </Grid>
    </AdornerDecorator>
</ControlTemplate>
于 2012-07-27T14:50:17.043 に答える