0

次の例を使用していますが、検証を行う必要があるリストは現在

検証ルールクラスの内部ですが、外部から取得する必要があり、リストは RT 中に変更できます。

ビューモデルから検証ルールクラスにリストを送信するにはどうすればよいですか

 public class PropertVal : ValidationRule
    {
        private readonly List<String>  validValues = new List<String> { "aa", "bb", "cc", "dd" };
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if(value == null)
                return new ValidationResult(false, "The Field are not match");

            string val = value.ToString().Trim();
            bool isValid = !string.IsNullOrEmpty(val) && validValues.Contains(val);
            ValidationResult result = null;
            result = isValid
                         ? new ValidationResult(true, null)
                         : new ValidationResult(false, "The Field are not match");

            return result;
        }
    }

XAML

<TextBox>
    <TextBox.Text>
        <Binding Path="NameOfViewModelPropery" UpdateSourceTrigger="PropertyChanged"
                 >
            <Binding.ValidationRules>
                <local:PropertiesMapValidation ValidatesOnTargetUpdated="True"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>
4

2 に答える 2

0

解決策はありますが、テストする必要があります。まず、バインディングでプロパティ検証を直接使用することはできません。これは、クラスの新しいインスタンスを作成し、検証リストに項目を追加できないためです。App.xaml ファイルのリソース ベースにインスタンスを作成します。

<Application x:Class="WpfApplicationTest.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:WpfApplicationTest="clr-namespace:WpfApplicationTest" StartupUri="MainWindow.xaml">
<Application.Resources>
    <WpfApplicationTest:PropertVal x:Key="propertVal" />
</Application.Resources>

このようにして、アプリケーションのどの部分からでもアクセスできます。これは、私が作成した MainViewModel クラスの例です。

public class MainViewModel
{
    public void AddValue()
    {
        var propVal = App.Current.MainWindow.Resources["propertVal"] as PropertVal; 
        if (propVal == null)
            return;
        propVal.AddListItem("Some New Item");
    }
}

PropertValしかし、これを行うには、クラスにパブリック メソッドを作成する必要があります。

public class PropertVal:ValidationRule
{
    private readonly List<String> validValues = new List<String> { "aa", "bb", "cc", "dd" };
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (value == null)
            return new ValidationResult(false, "The Field are not match");

        string val = value.ToString().Trim();
        bool isValid = !string.IsNullOrEmpty(val) && validValues.Contains(val);
        ValidationResult result = null;
        result = isValid
                     ? new ValidationResult(true, null)
                     : new ValidationResult(false, "The Field are not match");

        return result;
    }

    public void AddListItem(string item)
    {
        //...
    }

    public void RemoveItem(string item)
    {
        //...
    }
}

次に、xaml コードでそれを使用します。

<TextBox>
        <TextBox.Text>
            <Binding Path="NameOfViewModelPropery" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <StaticResource ResourceKey="propertVal"/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

それが役に立てば幸い。これは私が完全にテストしたわけではありませんが、役立つことを願っています...

于 2014-01-29T13:41:16.593 に答える