无绑定的ComboBox验证 [英] ComboBox Validation without Binding

查看:103
本文介绍了无绑定的ComboBox验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题基于此解决方案,但我无法使用ComboBox工作。我想验证点击提交按钮时,组合框有一个选定的项,并且不为null。请注意,我不是有意义的任何东西,而不是因为我不知道如何。但是如果答案是没有办法我可以使用验证规则没有绑定(特别是对ComboBoxes,我已经通过链接解决方案到文本框),请让我知道。

This question is based on this solution, but I somehow cannot get this to work using a ComboBox. I would like to validate that upon clicking a submit button, the combobox has a selected item and is not null. Please note that I am not binding to anything on purpose, and not because I don't know how to. But if the answer is that there is no way I can use the validation rules without binding (to ComboBoxes specifically, I've done it to textboxes via the linked solution), please let me know.

这里是我到目前为止:

XAML:

<ComboBox DataContext="{StaticResource ChargeAssigneeViewSource}" Name="ChargeAssigneeBox" ItemsSource="{Binding}" Width="85">
    <ComboBox.SelectedItem>
        <Binding RelativeSource="{RelativeSource Self}" Path="GetType" Mode="TwoWay">
            <Binding.ValidationRules>
                <my:ComboBoxValidationRule ErrorMessage="Please select an Assignee" />
            </Binding.ValidationRules>
        </Binding>
    </ComboBox.SelectedItem>
</ComboBox>

验证规则:

class ComboBoxValidationRule : ValidationRule
{
    private string errorMessage;

    public string ErrorMessage
    {
        get { return errorMessage; }
        set { errorMessage = value; }
    }
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (value == null)

            return new ValidationResult(false, ErrorMessage);

        return new ValidationResult(true, null);

    }
}

按钮点击:

private void AddAnHours()
    {
        Employee currEmployee;

        if (!ValidateElement.HasError(ChargeAssigneeBox))
        {
            if (!ValidateElement.HasError(analystTimeTxtBox))
            {
                currEmployee = ChargeAssigneeBox.SelectedItem as Employee;
                item.AddTime(currEmployee, DateTime.Now, double.Parse(analystTimeTxtBox.Text));
                analystTimeTxtBox.Clear();
                ChargeAssigneeBox.SelectedItem = null;
            }
        }

        UpdateTotals();

    }

我得到的错误在这行: p>

The error that I get is in this line:

currEmployee = ChargeAssigneeBox.SelectedItem as Employee;

但我的ItemsSource正在绑定,所以即使我选择了一个项目,selecteditem不转换到员工对象。我怀疑它与我绑定到它有什么关系:

but my ItemsSource is binding properly so even though I have selected an item, the selecteditem is not converting it to an employee object. I suspect it has something to do with what I am binding it to:

<Binding RelativeSoruce="{RelativeSource Self}" Path="GetType"....>

非常感谢您的帮助。

推荐答案

您不能将属性绑定到组合框上的selectedItem,然后检查该属性是否为null?

You can't bind a property to the selectedItem on the combobox and then check if thats property is null?

   Public string SelectedComboboxItem { get; set; }


     <ComboBox DataContext="{StaticResource ChargeAssigneeViewSource}"  SelectedItem="{Binding SelectedComboboxItem}" Name="ChargeAssigneeBox" ItemsSource="{Binding}" Width="85">

这篇关于无绑定的ComboBox验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆