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

时间:2015-07-23 15:37:38

标签: c# validation binding combobox win-universal-app

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

如果两个组合框都没有选择,我尝试禁用添加按钮的启用属性,但如果只选择了其中一个组合框,则会重新启用该按钮。

有人可以建议修复这种情况,或指出我在这个设置上出错了吗?

按钮的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}}" />

这是我的ViewModel缩小版:

命名空间LC_Points.ViewModel {     公共类MainViewModel:ViewModelBase     {         private readonly IRepository _repository = App.ScoresRepository;

    public MainViewModel()
    {
        //call methods to initilise list data
        InitSubjectTypes();
        InitOrdinaryGradePairs();
        InitHigherGradePairs();
    }

    public List<ScoreModel> Subjects { get; set; }
    public List<StringKeyValue> HigherGradePointKV { get; set; }
    public List<StringKeyValue> OrdinaryGradePointKV { get; set; }

    //Button enabled binding set based on the combo boxes being selected -->
    private ScoreModel _selectedSubject;
    public ScoreModel SelectedSubject
    {
        get { return _selectedSubject; }
        set
        {
            if (value != _selectedSubject)
            {
                _selectedSubject = value;
                RaisePropertyChanged("SelectedSubject");
                //I set the buttons enabled property to true is the combo box has been selected, 

但我只希望在选择了两个组合框后启用它---&gt;                     ButtonEnabled = true;                 }                 其他                 {                     ButtonEnabled = false;                 }             }         }

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

}

}

1 个答案:

答案 0 :(得分:2)

使用MultiBinding for Button IsEnabled,如下所示,

  <Button.IsEnabled>
     <MultiBinding Converter="{StaticResource CheckIfBothSelectedMultiConverter}">
                <Binding Path="Combo1" />
                <Binding Path="Combo2" />
            </MultiBinding>
</Button.IsEnabled>

检查CheckIfBothSelectedMultiConverter转换器中的条件,如果两个值都为真,则返回true,否则返回false。

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
}
}

这是我在Win RT / Universal App中获得Multi Binding的原因

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