C# 如何在从组合框添加数据时检查有效选择?

C# 如何在从组合框添加数据时检查有效选择?,c#,validation,binding,combobox,win-universal-app,C#,Validation,Binding,Combobox,Win Universal App,我的视图上有两个组合框和一个添加按钮,两个组合框都必须从中选择一个项目,才能有有效的选择 如果两个组合框都没有选择,我尝试禁用add button的enabled属性,但当前如果只选择了一个组合框,则按钮将重新启用 有人能为这种情况提出解决方案吗,或者指出我在这个设置中哪里出了问题 按钮的启用属性绑定到ViewModel中的属性: //This button enable property is bound to the combo boxes being selected ----->

我的视图上有两个组合框和一个添加按钮,两个组合框都必须从中选择一个项目,才能有有效的选择

如果两个组合框都没有选择,我尝试禁用add button的enabled属性,但当前如果只选择了一个组合框,则按钮将重新启用

有人能为这种情况提出解决方案吗,或者指出我在这个设置中哪里出了问题

按钮的启用属性绑定到ViewModel中的属性:

 //This button enable property is bound to the combo boxes being selected  ----->
        <Button x:Name="addGradeBtn"
                Grid.Row="2"
                HorizontalAlignment="Left"
                Command="{Binding Path=AddGradeCommand}"
                Content="Add Grade"
                IsEnabled="{Binding ButtonEnabled,
                                    Mode=TwoWay}" />

        <ComboBox x:Name="subjectCmbBx"
                  Grid.Row="1"
                  Grid.ColumnSpan="2"
                  Width="199"
                  HorizontalAlignment="Left"
                  VerticalAlignment="Top"
                  DisplayMemberPath="Subject"
                  Header="Subjects"
                  ItemsSource="{Binding Subjects}"
                  PlaceholderText="Pick a subject"
                  SelectedItem="{Binding SelectedSubject,
                                         Mode=TwoWay}" />


        <ComboBox x:Name="ordinaryGradeCmbBx"
                  Grid.Row="1"
                  Grid.Column="0"
                  Grid.ColumnSpan="2"
                  Width="170"
                  HorizontalAlignment="Right"
                  DisplayMemberPath="Key"
                  Header="Grades"
                  ItemsSource="{Binding OrdinaryGradePointKV}"
                  PlaceholderText="Pick a grade"
                  SelectedValue="{Binding SelectedOrdinaryGrade,
                                          Mode=TwoWay}"
                  Visibility="{Binding IsHigher,
                                       Mode=TwoWay,
                                       Converter={StaticResource BoolToNonVisibilityConverter}}" />

        <ComboBox x:Name="higherGradeCmbBx"
                  Grid.Row="1"
                  Grid.Column="0"
                  Grid.ColumnSpan="2"
                  Width="170"
                  HorizontalAlignment="Right"
                  DisplayMemberPath="Key"
                  Header="Grades"
                  ItemsSource="{Binding HigherGradePointKV}"
                  PlaceholderText="Pick a grade"
                  SelectedValue="{Binding SelectedHigherGrade,
                                          Mode=TwoWay}"
                  Visibility="{Binding IsHigher,
                                       Mode=TwoWay,
                                       Converter={StaticResource BoolToVisibilityConverter}}" />

}

对按钮启用使用多重绑定,如下所示

    private StringKeyValue _selectedHigherGrade;
    public StringKeyValue SelectedHigherGrade
    {
        get { return _selectedHigherGrade; }
        set
        {
            if (value != _selectedHigherGrade)
            {
                _selectedHigherGrade = value;
                RaisePropertyChanged("SelectedHigherGrade");
                ButtonEnabled = true;
            }
            else
            {
                ButtonEnabled = false;
            }
        }
    }

    private StringKeyValue _selectedOrdinaryGrade;
    public StringKeyValue SelectedOrdinaryGrade
    {
        get { return _selectedOrdinaryGrade; }
        set
        {
            if (value != _selectedOrdinaryGrade)
            {
                _selectedOrdinaryGrade = value;
                RaisePropertyChanged("SelectedOrdinaryGrade");
                ButtonEnabled = true;

            }
            else
            {
                ButtonEnabled = false;
            }
        }
    }

}
  <Button.IsEnabled>
     <MultiBinding Converter="{StaticResource CheckIfBothSelectedMultiConverter}">
                <Binding Path="Combo1" />
                <Binding Path="Combo2" />
            </MultiBinding>
</Button.IsEnabled>
这就是我在Win RT/Universal应用程序中获得的多绑定

 <TextBlock FontSize="20" TextWrapping="Wrap" Foreground="Cyan">
  <mb:MultiBindingLinker.Attach>
   <mb:MultiBindings>
    <mb:MultiBinding TargetProperty="Text" Converter="{StaticResource ConcatMultiConverter}">
      <mb:Binding Path="StringValue" />
      <mb:Binding Path="Text" ElementName="ConcatTextBox1"/>
      <mb:Binding Path="Text" ElementName="ConcatTextBox2" Converter="{StaticResource ToUpperCaseConverter}"/>
      <mb:Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
 </mb:MultiBinding>
 </mb:MultiBindings>
 </mb:MultiBindingLinker.Attach>
</TextBlock>


我添加了converter类,但出现了两个错误,1.找不到IMultiValueConverter,并且没有导入它的选项,这是一个Windows RT项目,有其他选择吗?2.“Convert”是一个在当前上下文中无效的方法,这在“if”语句中给出。包括名称空间System.Windows.Data for IMultiValueConverter,这两个错误都将消失。更新了问号,这是一个Windows RT/Universal项目而不是WPF,这就是为什么我会出现这些错误,是否有其他选择?Ohh。但您仍然可以使用IMultivalueConverter,因为您正在使用转换器。。。Converter支持IValueConverter和IValueConvertor,所以我相信您可以实现它。
public class IsEnabledCheckConverter : IMultiValueConverter
{  
    public object Convert(object[ ] values, Type targetType, object parameter, CultureInfo culture)
{
      if(Convert.ToBoolean(values[0]) && Convert.ToBoolean(values[1]))
        {
            return true;
        }
     return false;        
}

public object Convert(object[ ] values, Type targetType, object parameter, CultureInfo culture)
{
     //convert back Logic
}
}
 <TextBlock FontSize="20" TextWrapping="Wrap" Foreground="Cyan">
  <mb:MultiBindingLinker.Attach>
   <mb:MultiBindings>
    <mb:MultiBinding TargetProperty="Text" Converter="{StaticResource ConcatMultiConverter}">
      <mb:Binding Path="StringValue" />
      <mb:Binding Path="Text" ElementName="ConcatTextBox1"/>
      <mb:Binding Path="Text" ElementName="ConcatTextBox2" Converter="{StaticResource ToUpperCaseConverter}"/>
      <mb:Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}" />
 </mb:MultiBinding>
 </mb:MultiBindings>
 </mb:MultiBindingLinker.Attach>
</TextBlock>