2009-05-23 2 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

또한 또 다른 방법, 당신은 있는지 확인하지 않은 경우 label가 null의 경우, label가로드되기 전에 윈도우가로드되면 선택 변경 핸들러를 추가합니다. 뒤에

코드 :

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 (+1) :-) –

+0

XAML에 대한 좋은 점은 무언가를 할 수있는 50 가지 방법이나 0이 있다는 것입니다! ;) – Scott

관련 문제