이 질문은 this solution을 기반으로하지만, 어떻게 든이 ComboBox를 사용하여 작동시키지 못합니다. 제출 버튼을 클릭 할 때 콤보 상자에 선택된 항목이 있고 null이 아닌지 확인하고 싶습니다. 제가 의도적으로 구속력이있는 것이 아니라, 어떻게 해야할지 모르기 때문이 아닙니다. 하지만 그 대답은 바인딩 규칙없이 유효성 검사 규칙을 사용할 수있는 방법이 없다면 (구체적으로 ComboBoxes에, 연결된 솔루션을 통해 텍스트 상자에 입력했습니다.) 알려 주시기 바랍니다.바인딩없이 ComboBox 유효성 검사
는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();
}
내가 오류를 여기
는 내가 지금까지 무엇을 가지고 이 줄에 :내 ItemsSource가 제대로 바인딩되어 있으므로 항목을 선택했는데 selectedItem이 해당 항목을 직원 개체로 변환하지 않습니다. 나는 그것을 묶는 것과 관련이 있다고 생각한다 :
<Binding RelativeSoruce="{RelativeSource Self}" Path="GetType"....>
도움을 주시면 감사하겠습니다.
바인딩이없는 모든 콤보 상자 항목에 대해이 작업을 계속하고 싶지 않습니다. 그리고 매번 같은 항목에 묶어두기를 원하지 않습니다. 이것이 불가능할 경우, 그렇게 말하면, 그렇지 않으면이 솔루션 유형을 포함 할 때 답변을 얻을 수 없습니다. – Erika