2

私はかなり混乱した状況に陥っています:

INotifyDataErrorInfoすぐにエラーを返す (テキスト フィールドが空でない場合)ビューを表示するダイアログを開くと、赤い境界線のエラー通知が表示されます。

オープニング #1:

最初のオープニング

何もせずにウィンドウを閉じてから、開くボタンをもう一度クリックします。

オープニング #2:

セカンドオープニング

一体何?エラーフラグを確認しましたが、設定されています。テキストを削除して何かを書き戻すと、エラー境界が再び表示されます。string empty? error: no error

ここに小さな再現ケースがあります:

編集:すべてのショーで作成されるViewModelを元に戻し、INCP変更イベントを引き起こしました

MainWindow.xaml.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;

namespace LayoutBreakerMinimal
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ButtonBase_OnClick( object sender, RoutedEventArgs e )
        {
            var w = new Window();
            var v = Resources["OneInstanceView"] as View; // new View(); <-- would work
            w.Content = v;
            v.DataContext = new ViewModel();
            w.ShowDialog();
        }
    }


    public partial class View : UserControl
    {
        public View()
        {
            InitializeComponent();
        }
    }


    public partial class ViewModel : INotifyDataErrorInfo, INotifyPropertyChanged
    {
        private string _myTextField;

        public ViewModel()
        {
            MyTextField = "Error field";
        }

        public string MyTextField
        {
            get { return _myTextField; }
            set
            {
                _myTextField = value;
                OnPropertyChanged();
                if ( ErrorsChanged != null ) ErrorsChanged( this, new DataErrorsChangedEventArgs( "MyTextField" ) );
            }
        }

        public IEnumerable GetErrors( string propertyName )
        {
            yield return "Field is null";
        }

        public bool HasErrors
        {
            get { return MyTextField != ""; }
        }

        public event EventHandler< DataErrorsChangedEventArgs > ErrorsChanged;

        protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
        {
            var handler = PropertyChanged;
            if ( handler != null ) handler( this, new PropertyChangedEventArgs( propertyName ) );
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

MainWindow.xaml

<Window x:Class="LayoutBreakerMinimal.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:layoutBreakerMinimal="clr-namespace:LayoutBreakerMinimal"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <layoutBreakerMinimal:View x:Key="OneInstanceView" />
    </Window.Resources>
    <Grid>

        <Button Click="ButtonBase_OnClick" Margin="40">Open Dialog, then open it again</Button>

    </Grid>
</Window>

View.xaml

<UserControl x:Class="LayoutBreakerMinimal.View"
             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" 
             d:DesignHeight="300" d:DesignWidth="300">
 <StackPanel>
    <TextBox Text="{Binding MyTextField, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" Height="30" Width="100"></TextBox>
    <Label Content="{Binding HasErrors}" Height="30" Width="100"></Label>
 </StackPanel>
</UserControl>

境界線が消える理由がわかりません。

私が発見したこと: 毎回新しい (単一のリソース インスタンスではなく) ビューを作成すると、毎回最初から赤い境界線が使用可能になります。

new のたびにインスタンス化INotifyDataErrorInfoされる別の に移動することをテストしました->運がありません。ViewModel

編集 2: エラーを表示し続けることを示すために、ビューに HasError ラベルを追加しました

4

1 に答える 1