我想将验证类的ItemsSource对象交给我,以检查是否可以在(ItemsSource的)列表中找到用户输入的内容(处理程序类中的值)。

如何将ComboBox的ItemsSource提交到处理程序类RestrictedComboBoxItemValidationRule?
(或我的ComboBox-controll而不是ItemsSource)

<ComboBox Name="bms_ComboBox
          ItemsSource='{Binding Path="[BMS,ANR]"}'
          SelectedValuePath="F1"
          DisplayMemberPath="F1"
          IsEditable="True">
    <ComboBox.Text>
        <Binding Path="[BMS]">
            <Binding.ValidationRules>
                <t:RestrictedComboBoxItemValidationRule Sender={how I can submit ItemsSource of this ComboBox to handler class???}/>
            </Binding.ValidationRules>
        </Binding>
    </ComboBox.Text>
</ComboBox>

// ...

public class RestrictedComboBoxItemValidationRule : ValidationRule
{
    public object Sender
    {
        get { return sender; }
        set { sender = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        ValidationResult vr = ValidationResult.ValidResult;

        if (comboText_inItemsSource == false) {
            vr = new ValidationResult(false, "The entered value is not included in list!");
        }
        return vr;
    }

最佳答案

您需要将ComboBox的ItemsSource绑定到您的发件人属性。由于ValidationRule不是DependencyObject,因此不能仅将Sender设置为DependencyProperty。但是Josh Smith提供了一个不错的解决方法:http://www.codeproject.com/Articles/18678/Attaching-a-Virtual-Branch-to-the-Logical-Tree-in

关于c# - 将ComboBox的ItemsSource提交到ValidationRule,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13525180/

10-12 04:48