Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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#_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 是否可以在执行绑定功能之前执行帮助器方法?

C# 是否可以在执行绑定功能之前执行帮助器方法?,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我想在将用户输入的数据绑定到文本框之前检查业务规则?在将数据绑定到模型之前,我需要确保它满足特定标准。如果我能在绑定发生之前执行一个方法,这将非常容易做到。有什么方法可以让我这样做吗?你可以用。您将编写的代码将在将绑定值推送到UI之前执行。这实际上是转换器的含义:给你在BIDIN机制中间注入代码的能力(在执行UI之前或之后)可以使用。您将编写的代码将在将绑定值推送到UI之前执行。这实际上是转换器的意义:给你在BIDIN机制中间注入代码的能力(在执行UI之前或之后)< P>你可以实现: 并在您的绑

我想在将用户输入的数据绑定到文本框之前检查业务规则?在将数据绑定到模型之前,我需要确保它满足特定标准。如果我能在绑定发生之前执行一个方法,这将非常容易做到。有什么方法可以让我这样做吗?

你可以用。您将编写的代码将在将绑定值推送到UI之前执行。这实际上是转换器的含义:给你在BIDIN机制中间注入代码的能力(在执行UI之前或之后)

可以使用。您将编写的代码将在将绑定值推送到UI之前执行。这实际上是转换器的意义:给你在BIDIN机制中间注入代码的能力(在执行UI之前或之后)

< P>你可以实现:

并在您的绑定中使用它:

<TextBox>
    <TextBox.Text>
        <Binding Path="ViewModelProperty" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validation:CustomValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

其中
validation
是定义了
CustomValidationRule
的命名空间的XML命名空间别名(将
xmlns:validation=“clr namespace:namespace\u NAME\u HERE”
添加到XAML中)。

您可以实现:

并在您的绑定中使用它:

<TextBox>
    <TextBox.Text>
        <Binding Path="ViewModelProperty" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validation:CustomValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>


其中
validation
是定义了
CustomValidationRule
的命名空间的XML命名空间别名(将
xmlns:validation=“clr namespace:namespace\u NAME\u HERE”
添加到XAML中)。

您可以让绑定到视图的ViewModel实现IDataErrorInfo。 此接口具有属性和索引器:

public string this[string columnName]
{
    //The validation logic goes here
    if( columnName == "Property1")
    {
        //put validation here and return error message if exists
        if(this.Property1 == "")
        {
             return "The field Property1 is required";
        }
    }
    //and so on
}

public string Error
{
    return "This object is not valid";
}
在视图的绑定中,向绑定标记添加以下内容:

<TextBox Text={Binding Property1, ValidatesOnDataErrors=True} />

别忘了通知你的财产发生了变化


希望这有帮助。

您可以让绑定到视图的ViewModel实现IDataErrorInfo。 此接口具有属性和索引器:

public string this[string columnName]
{
    //The validation logic goes here
    if( columnName == "Property1")
    {
        //put validation here and return error message if exists
        if(this.Property1 == "")
        {
             return "The field Property1 is required";
        }
    }
    //and so on
}

public string Error
{
    return "This object is not valid";
}
在视图的绑定中,向绑定标记添加以下内容:

<TextBox Text={Binding Property1, ValidatesOnDataErrors=True} />

别忘了通知你的财产发生了变化

希望这是有帮助的