Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在自己的控件中传递验证错误_C#_Silverlight_Xaml_Mvvm - Fatal编程技术网

C# 在自己的控件中传递验证错误

C# 在自己的控件中传递验证错误,c#,silverlight,xaml,mvvm,C#,Silverlight,Xaml,Mvvm,我有自己的用户控制: [TemplateVisualState(Name = StateValid, GroupName = GroupValidation)] [TemplateVisualState(Name = StateInvalidFocused, GroupName = GroupValidation)] [TemplateVisualState(Name = StateInvalidUnfocused, GroupName = GroupValidation)] public cl

我有自己的用户控制:

[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”验证服务(我希望控件文本框上有相同的验证错误工具提示)?
亲切的问候

您可以实现IDataErrorInfo接口。它在ComponentModel库中可用

使用系统组件模型

公共类SearchTextBoxControl:TextBox,IDataErrorInfo

{


}您可以实现IDataErrorInfo接口。它在ComponentModel库中可用

使用系统组件模型

公共类SearchTextBoxControl:TextBox,IDataErrorInfo

{


}

不管怎样-你知道答案吗?:)不幸的是,可能不是。在WPF中,对于这样简单的事情,我将实现IDataErrorInfo,并执行ValidateSondaErrors操作。但我不确定Silverlight或使用可视状态时是否支持此功能。无论如何,您知道答案吗?:)不幸的是,可能不是。在WPF中,对于这样简单的事情,我将实现IDataErrorInfo,并执行ValidateSondaErrors操作。但我不确定Silverlight或使用可视状态时是否支持此功能。这不完全是我要找的,但thanx:)这不完全是我要找的,但thanx:)
    #region IDataErrorInfo Members

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

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

    #endregion
    // Your Code