Использование проверки данных в ControlTemplate

174
21 мая 2018, 17:40

Пытаюсь сформировать проверку данных в Wpf. Примеры из нета работают. Но когда хочу переместить все шаблон не визуально не показывает ошибку. Код модели:

public class Model: INotifyPropertyChanged, IDataErrorInfo
{
    #region Members
    private string _name;
    public string Name {
        get { return _name; }
        set { _name = value; this.SendPropertyChanged("Name"); }
    }
    private int _index;
    public int Index {
        get { return _index; }
        set { _index = value; this.SendPropertyChanged("Index"); }
    }
    #endregion
    #region Members IDataErrorInfo
    private Dictionary<string, string> _errors = new Dictionary<string, string>();
    public string Error
    {
        get { return string.Join<string>(Environment.NewLine, _errors.Select(x => x.Value)); }
    }
    public string this[string columnName]
    {
        get
        {
            string error = string.Empty;
            switch (columnName)
            {
                case "Name":
                    if (Name == null || Name.Length == 0)
                        error = _errors["Name"] = "value by not emprty";
                    else
                        _errors.Remove("Name");
                    break;
                case "Index":
                    if (Index == 0)
                        error = _errors["Index"] = "Index by less zero";
                    else
                        _errors.Remove("Index");
                    break;
            }
            return error;
        }
    } 
    #endregion
    #region Members INotifyPropertyChanged
    public event PropertyChangedEventHandler PropertyChanged;
    protected void SendPropertyChanged(string propertyName = "") {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion
}

Разметка в App:

    <Application.Resources>
    <ResourceDictionary>
        <ControlTemplate x:Key="winTemplate" TargetType="{x:Type Window}">
            <Grid Background="{TemplateBinding Background}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                    <RowDefinition Height="50" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="10" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <PasswordBox Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="20, 0" >
                    <b:PasswordBoxBehaviour.Password>
                        <Binding Path="Name" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                            <Binding.ValidationRules>
                                <DataErrorValidationRule />
                            </Binding.ValidationRules>
                        </Binding>
                    </b:PasswordBoxBehaviour.Password>
                </PasswordBox>
                <TextBox Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Margin="20, 0" >
                    <TextBox.Text>
                        <Binding Path="Index" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
                            <Binding.ValidationRules>
                                <DataErrorValidationRule ValidatesOnTargetUpdated="True" />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
            </Grid>
        </ControlTemplate>
    </ResourceDictionary>
</Application.Resources>

В итоге не отображаются ошибки:

Подскажите в чем может быть проблема.

READ ALSO
Создать Excel файл

Создать Excel файл

Как создатьxls файл без установленного экселя? Обрыл все, ненашел никаких вариантов кроме Microsoft

176
Отключить TRACE библиотеке

Отключить TRACE библиотеке

Отключаю Define TRACE constant в вкладке Build для моего приложенияНо библиотека продолжает трассировку в консоли

171
Как добавить скроллбары в кастомный RichTextBox?

Как добавить скроллбары в кастомный RichTextBox?

Создаю свою компоненту richtextboxНе могу понять как добавить вертикальный скроллбар?

230
Не работает привязка DataGridComboBoxColumn ItemSource к списку

Не работает привязка DataGridComboBoxColumn ItemSource к списку

К Datagrid подключается коллекция с одним элементомОтображается только ячейка с датой

215