Другой способ также, если вы не хотите проверять, является ли метка нулевой, - это добавить обработчик изменения выбора после загрузки окна, так как он будет срабатывать до загрузки метки:
Код сзади:
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.SelectionChanged+=new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}
protected void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
label1.Content = ((ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex]).Content;
}
Markup:
<Grid>
<ComboBox Height="23" Margin="139,0,19,14" Name="comboBox1" Text="Worker"
VerticalAlignment="Bottom" IsReadOnly="True">
<ComboBoxItem Name="None" Selector.IsSelected="True">Select Target</ComboBoxItem>
<ComboBoxItem Name="Alice">Alice</ComboBoxItem>
<ComboBoxItem Name="Bob">Bob</ComboBoxItem>
<ComboBoxItem Name="Chris">Chris</ComboBoxItem>
<ComboBoxItem Name="Dan">Dan</ComboBoxItem>
</ComboBox>
<Label Height="28" Margin="15,0,0,14" Name="label1"
VerticalAlignment="Bottom" Content="Assign to: " HorizontalAlignment="Left" Width="120"></Label>
</Grid>
Andrew