ComboBox的Text属性具有最后选择的值,而不是当前选择

时间:2012-03-08 22:18:51

标签: c# wpf xaml

我在WPF页面上有一个标签LabelMessage和一个ComboBox ComboBoxSelection。这是XAML:

<Label Name="LabelMessage" Content="" Margin="0,20" HorizontalAlignment="Stretch" VerticalAlignment="Center" />
<ComboBox x:Name="ComboBoxSelection" SelectionChanged="OnComboBoxSelectionChanged" Height="20" Margin="112,10,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Top" IsEnabled="True" >
    <ComboBoxItem Content="North" />
    <ComboBoxItem Content="South" />
    <ComboBoxItem Content="East" />
    <ComboBoxItem Content="West" />
</ComboBox>

这是C#:

private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    LabelMessage.Content = ComboBoxSelection.Text;
}

标签显示ComboBox的之前内容,而不是当前选择。谢谢,如果我选择North然后选择East,则标签会显示North。如果我然后选择South,则标签会显示East

可能导致此问题的原因,我该如何解决?

感谢。

2 个答案:

答案 0 :(得分:2)

使用以下内容:ComboBoxSelection.SelectedItem.ToString();

答案 1 :(得分:0)

解决方案:

LabelMessage.Content = ComboBoxSelection.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", "");