0

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.

Here is what I have so far:

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>

Validation Rule:

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);

    }
}

Button Click:

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();

    }

The error that I get is in this line:

currEmployee = ChargeAssigneeBox.SelectedItem as Employee;

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"....>

Help would be greatly appreciated, thanks.

Community
  • 1
  • 1
Erika
  • 289
  • 1
  • 5
  • 20

1 Answers1

0

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">
TMan
  • 4,044
  • 18
  • 63
  • 117
  • I don't want to keep doing this for every combobox Item that has no binding in it. And I don't want to keep binding it to the same item every single time. If this is impossible just say so, otherwise I can't take answers when it includes this type of solution. – Erika Jul 27 '12 at 19:50