2009-05-23 77 views
0

我不相信這個:只用一個組合框創建了一個非常簡單的表單,當用戶選擇一個項目時,標籤將顯示選擇。這裏是我的代碼:WPF:XamlParserException對於一個非常簡單的表單?

<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> 

後面的代碼:

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

回答

0

另一種方式也是,如果你不想檢查該標籤爲空,在窗口加載後添加選擇更改處理程序,因爲它會在標籤加載之前觸發一個:

代碼背後:

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; 
    } 

標記:

<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> 

安德魯

0

以下工作:

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

安德魯

1

下面是一個簡化版本都在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

尼斯的答案。可以將內容用於選定的文本:Content =「{Binding Name}」。從我+1 :-) – 2009-05-23 14:58:24