从组合框中添加数据时如何检查有效选择?
How to check valid selection when adding data from combo boxes?
我的视图上有两个组合框和一个添加按钮,两个组合框都必须从中选择一个项目才能进行有效选择。
如果两个组合框都没有选择,我尝试禁用添加按钮的启用 属性,但目前如果只有一个组合框被选中,按钮会重新启用。
任何人都可以针对这种情况提出修复建议,或者指出我在这个设置中哪里出了问题吗?
按钮的启用 属性 绑定到 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
{
public class MainViewModel:ViewModelBase
{
私人只读 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,
但我只希望在两个组合框都被选中时启用它 --->
按钮启用=真;
}
别的
{
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;
}
}
}
}
}
像下面这样为 Button IsEnabled 使用 MultiBinding,
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource CheckIfBothSelectedMultiConverter}">
<Binding Path="Combo1" />
<Binding Path="Combo2" />
</MultiBinding>
</Button.IsEnabled>
检查 CheckIfBothSelectedMultiConverter 转换器中的条件,如果两个值都为真 return 为真,否则为假。
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
中获得的多重绑定
<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>
我的视图上有两个组合框和一个添加按钮,两个组合框都必须从中选择一个项目才能进行有效选择。
如果两个组合框都没有选择,我尝试禁用添加按钮的启用 属性,但目前如果只有一个组合框被选中,按钮会重新启用。
任何人都可以针对这种情况提出修复建议,或者指出我在这个设置中哪里出了问题吗?
按钮的启用 属性 绑定到 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 { public class MainViewModel:ViewModelBase { 私人只读 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,
但我只希望在两个组合框都被选中时启用它 ---> 按钮启用=真; } 别的 { 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;
}
}
}
}
}
像下面这样为 Button IsEnabled 使用 MultiBinding,
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource CheckIfBothSelectedMultiConverter}">
<Binding Path="Combo1" />
<Binding Path="Combo2" />
</MultiBinding>
</Button.IsEnabled>
检查 CheckIfBothSelectedMultiConverter 转换器中的条件,如果两个值都为真 return 为真,否则为假。
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
中获得的多重绑定 <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>