2013-07-01 2 views
0

나는 C#의 기초를 배우고있다. WPF를 사용하고 있습니다. 목록 상자를 선택하여 사라지게하려면 목록 상자를 만들고 싶습니다. 난 = 가시성을 사용 붕괴하지만 내 코드가 여기에 작동하지 않습니다 :목록 상자에서 항목을 선택한 후 목록 상자를 숨기는 방법은 무엇입니까?

<ListBox Foreground="White" Grid.Row="1" SelectionMode="Single" SelectionChanged="PrintText" Background="DarkGray" Visibility="Collapsed" Height="Auto" HorizontalAlignment="Left" Margin="156,36,0,0" Name="listBox1" VerticalAlignment="Top" Width="191" UseLayoutRounding="True" /> 

private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    if(autolist.Count>0) 
    { 
     listBox1.ItemsSource = autolist; 
     listBox1.Visibility = Visibility.Visible; 
     // a = pk; 
    } 
    else 
    { 
     listBox1.Visibility = Visibility.Collapsed; 
     listBox1.ItemsSource = null; 
    } 
} 

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    //selectedItemsId = (int)listBox1.SelectedValue; 
    if (listBox1.ItemsSource != null) 
    { 
     listBox1.Visibility = Visibility.Collapsed; 
     textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged); 
    } 

    if (listBox1.SelectedIndex != -1) 
    { 
     textBox1.Text = listBox1.SelectedItem.ToString(); 
     textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged); 
    } 
} 
+0

이 인 목록 상자 이벤트 처리기에서이 같은 시도 asp.net? 또는 WPF? 이것은 또한 C# 5.0과 아무 관련이 없습니다. –

+1

코드에 여러 가지 끔찍한 것들이 있습니다. 왜 목록 상자에서 선택 항목이 변경 될 때마다 텍스트 상자에 추가 리스너를 추가합니까? –

+0

@TimothyGroote 목록 상자 정의를보고 그가 Visibility를 사용하고 있다고 가정하면 WPF라고 말하는 것이 안전합니다. – Tombala

답변

-1

당신은 쓸 수 있습니다 : 대신 listBox1.Visibility의

listBox1.Visible = false; 

합니다.

+0

"보이지 않음"과 "접힌 상태"사이에는 차이가 있습니다. http://stackoverflow.com/questions/886742/difference-between-visibility-collapsed-and-visibility-hidden을 참조하십시오. –

+0

또한 WPF의 UIElement에는 Visible이라는 속성이 없습니다 –

1

XAML에서 호출하는 것과 다른 이름을 가진 이벤트 처리기를 정의했기 때문에 아무 것도 일어나지 않습니다.

귀하의 목록 상자는 PrintText을 실행하려고 시도하지만 귀하의 코드에서 볼 수 있으며, 대신 listBox1_SelectionChanged을 발사하기를 원합니다.

이처럼 XAML을 변경

:

<ListBox Foreground="White" Grid.Row="1" SelectionMode="Single" SelectionChanged="listBox1_SelectionChanged" Background="DarkGray" Visibility="Collapsed" Height="Auto" HorizontalAlignment="Left" Margin="156,36,0,0" Name="listBox1" VerticalAlignment="Top" Width="191" UseLayoutRounding="True" /> 

을 또 다시 볼 수에 목록 상자를 설정에서 텍스트 상자 변경 이벤트를 방지하기 위해,

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      //selectedItemsId = (int)listBox1.SelectedValue; 
      if (listBox1.ItemsSource != null) 
      { 
       listBox1.Visibility = Visibility.Collapsed; 
      } 

      if (listBox1.SelectedIndex != -1) 
      { 
       //remove the listener on the textbox 
       textBox1.TextChanged -= TextBoxBase_OnTextChanged; 
       textBox1.Text = listBox1.SelectedItem.ToString(); 
       //put the listener back on the text box 
       textBox1.TextChanged += TextBoxBase_OnTextChanged; 
      } 
     } 
+0

Timothy가 여전히 작동하지 않습니다. –

+0

나는 놀랐다. 그래서 나는 당신의 코드를 시도한 다음 웃기 시작했다 .IT는 작동한다. 목록 상자를 숨긴다. 그러나 텍스트 상자의 값을 설정하여 textchanged 핸들러를 시작하게한다. –

+0

예제를 사용하여 답변을 업데이트했습니다. –

관련 문제