Передать ошибки проверки в собственном контроле - PullRequest
1 голос
/ 16 сентября 2011

У меня есть собственный пользовательский элемент управления:

[TemplateVisualState(Name = StateValid, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidFocused, GroupName = GroupValidation)]
[TemplateVisualState(Name = StateInvalidUnfocused, GroupName = GroupValidation)]
public class SearchTextBoxControl : TextBox
{
    // properties removed for brevity

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        this.BindingValidationError += (s, e) => UpdateValidationState();
        this.UpdateValidationState();
    }

    public const string GroupValidation = "ValidationStates";
    public const string StateValid = "Valid";
    public const string StateInvalidFocused = "InvalidFocused";
    public const string StateInvalidUnfocused = "InvalidUnfocused";

    private void UpdateValidationState()
    {
        var textBox = this.GetTemplateChild("ContentTextBox");
        if (textBox != null)
        {
            VisualStateManager
                .GoToState(textBox as Control, 
                           Validation.GetErrors(this).Any() ? 
                               StateInvalidUnfocused : 
                               StateValid, 
                           true);
        }
    }
}

и XAML:

<Style TargetType="local:SearchTextBoxControl">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:SearchTextBoxControl">
                <Grid Grid.Column="1"
                      Grid.ColumnSpan="3"
                      Grid.Row="1"
                      Margin="{TemplateBinding Margin}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="32" />
                    </Grid.ColumnDefinitions>

                    <TextBox x:Name="ContentTextBox"
                             Grid.ColumnSpan="2"
                             IsReadOnly="{TemplateBinding IsReadOnly}"
                             Text="{TemplateBinding Text}">
                    </TextBox>
                    <Button Grid.Column="1"
                            Style="{StaticResource BrowseButton}"
                            Command="{TemplateBinding Command}">
                        <ToolTipService.ToolTip>
                            <ToolTip Content="{TemplateBinding ToolTip}" />
                        </ToolTipService.ToolTip>
                        <Image  Source="../../Resources/Images/magnifier.png"
                                Style="{StaticResource BrowseButtonImage}" />
                    </Button>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Что я должен сделать, чтобы передать ошибки проверки в службу проверки TextBox x: Name = "ContentTextBox" (я хочу такую ​​же подсказку об ошибках проверки в моем текстовом поле управления)? С уважением!

1 Ответ

1 голос
/ 27 сентября 2011

Вы можете реализовать интерфейс IDataErrorInfo.Он доступен в ComponentModel Lib.

с использованием System.ComponentModel;

открытый класс SearchTextBoxControl: TextBox, IDataErrorInfo

{

    #region IDataErrorInfo Members

    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    public string this[string columnName]
    {
        get { throw new NotImplementedException(); }
    }

    #endregion
    // Your Code

}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...