2009-05-23 22 views
0

Ich glaube das nicht: Nur ein sehr einfaches Formular mit einer Combobox erstellt, wenn der Benutzer ein Element auswählt, zeigt das Etikett die Auswahl an. Hier ist mein Code:WPF: XamlParserException für eine sehr einfache Form?

<Window x:Class="WpfApplication8.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <ComboBox Height="23" Margin="139,0,19,14" Name="comboBox1" Text="Worker" 
        VerticalAlignment="Bottom" IsReadOnly="True" SelectionChanged="comboBox1_SelectionChanged"> 
      <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> 
</Window> 

-Code hinter:

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    label1.Content = comboBox1.SelectedValue; 
} 

Antwort

0

Eine andere Art und Weise auch, wenn Sie wollte nicht prüfen, ob Das Label ist null, es wird der Auswahländerungs-Handler hinzugefügt, sobald das Fenster geladen ist, da es einen auslöst, bevor das Label geladen wurde:

-Code Behind:

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

0

folgende Arbeiten:

protected void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (label1 != null) 
      label1.Content = ((ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex]).Content; 
    } 

Andrew

1

Hier ist eine vereinfachte Version alle in XAML:

<Page 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <StackPanel> 
      <ComboBox Name="comboBox1" Text="Worker" IsSynchronizedWithCurrentItem="True" 
        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 Name="label1" DataContext="{Binding ElementName=comboBox1, Path=SelectedItem}" 
       VerticalAlignment="Bottom" Content="{Binding Name}" HorizontalAlignment="Left"></Label> 

    </StackPanel> 
</Page> 
+0

Nizza Antwort. Kann für den ausgewählten Text aber auch Inhalt sein: Content = "{Binding Name}". +1 von mir :-) –

+0

Tolle Sache über XAML, gibt es 50 Möglichkeiten, etwas oder 0 zu tun! ;) – Scott