我制作了一个由datePicker
和comboBox
组成的应用程序。 comboBox
有一个从0到5的数字列表。当用户选择" 1"我想要启用datePicker
,但如果用户选择了其他任何内容,我希望datePicker
被禁用。但由于某些原因,此代码无效,datePicker
始终处于禁用状态。
我的代码是休闲
XAML
<ComboBox x:Name="cbxNoClaims" Loaded="cbxNoClaims_Loaded" Grid.Row="13" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120"/>
<DatePicker Name="dpkClaim1" Loaded="dpkClaim1_Loaded" Grid.Row="13" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Top"/>
MainWindow.xaml.cs
private void cbxNoClaims_Loaded(object sender, RoutedEventArgs e)
{
// ... A List.
List<string> claim = new List<string>();
claim.Add("0 ");
claim.Add("1");
claim.Add("2");
claim.Add("3");
claim.Add("4");
claim.Add("5");
// ... Get the ComboBox reference.
var comboBox = sender as ComboBox;
// ... Assign the ItemsSource to the List.
comboBox.ItemsSource = claim;
comboBox.SelectedIndex = 0;
}
private void dpkClaim1_Loaded(object sender, RoutedEventArgs e)
{
DatePicker datePicker = sender as DatePicker;
if ((string)cbxNoClaims.Item== "1")
{
dpkClaim1.IsEnabled = true;
}
else dpkClaim1.IsEnabled = false;
}
答案 0 :(得分:3)
在组合框中使用选择更改事件。
XAML:
<ComboBox x:Name="cbxNoClaims" SelectionChanged="cbxNoClaims_SelectionChanged" Grid.Row="13" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="120"/>
代码:
private void cbxNoClaims_SelectionChanged(object sender, RoutedEventArgs e)
{
if (cbxNoClaims.SelectedItem.ToString() == "1")
{
dpkClaim1.IsEnabled = true;
}
else
{
dpkClaim1.IsEnabled = false;
}
}