前台:
<ComboBox Height="23" Name="comB_Discount" Width="120" SelectionChanged="comB_Discount_SelectionChanged">
<ComboBoxItem Content="是" />
<ComboBoxItem Content="否" />
</ComboBox>
后台:
private void comB_Discount_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comB_Discount.Text == "是")
{
textB_zHour.IsReadOnly = false;
}
if (comB_Discount.Text == "否")
{
textB_zHour.IsReadOnly = true;
textB_zHour.Text = "0";
}
}
为什么我更改ComboBox的选项时,所取的值不对。。
因为你拿到的这个Text还是选项改变之前的Text,并不是选项改变后的Text~
可以这样写
private void comB_Discount_SelectionChanged(object sender, SelectionChangedEventArgs e) { string selectedContent = ((ComboBoxItem)comB_Discount.SelectedItem).Content.ToString(); if (selectedContent == "是") { textB_zHour.IsReadOnly = false; } if (selectedContent == "否") { textB_zHour.IsReadOnly = true; textB_zHour.Text = "0"; } }
textB_zHour只在"否"时赋值