2014-06-20 125 views
0

我想創建一個控件,顯示搜索結果作爲用戶在文本框中鍵入內容。爲此,我有一個文本框和一個彈出窗口,當用戶輸入內容時就會顯示出來(就像谷歌搜索框一樣)。像這樣,WPF與文本框彈出?

<Grid> <TextBox Name="userEntry" /> <Popup /> </Grid> 

現在,當用戶開始輸入到我想要的彈出顯示,直到用戶論點集中在一些其他的UI控件,或者如果輸入的文本是空的繼續開放的文本框中。 我無法輕鬆實現這一點,並想知道在wpf中是否有更好的方法來做到這一點。 問候

+1

替代的是什麼?你沒有展示你的方式,所以我們怎麼能知道什麼是替代方案? – icebat

+0

我做了...我想要一個谷歌搜索框功能,我在文本框中鍵入soemthing,我在其下面列出了結果..但我有麻煩焦點問題等顯示一個彈出窗口...另一種選擇我在尋找進入是...重新組合框來實現類似的事情。 – theOne

回答

0

XAML:

<Window> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <Button x:Name="btn" Content="Open Search Window" Height="30" Width="150" Click="btn_Click"/> 

     <Popup x:Name="popup" PlacementTarget="{Binding ElementName=btn}" Placement="Bottom" Width="200" Height="100" Margin="0,20,0,0"> 
      <Border BorderBrush="Black" BorderThickness="2" Background="AliceBlue"> 
       <TextBox x:Name="txtBox" VerticalAlignment="Center" Margin="15,0,15,0"/> 
      </Border> 
     </Popup> 

     <TextBox x:Name="focusTarger" Text="Focus Me !" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Width="200" TextAlignment="Center" FontSize="16"/> 

    </Grid> 
    </Window> 

CS:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     GotFocus += MainWindow_GotFocus; 
    } 

    void MainWindow_GotFocus(object sender, RoutedEventArgs e) 
    { 
     FrameworkElement element = (FrameworkElement)e.OriginalSource; 

     if (txtBox == element || popup == element || element.Parent == popup) 
      return; 

     popup.IsOpen = !string.IsNullOrEmpty(txtBox.Text); 
    } 

    private void btn_Click(object sender, RoutedEventArgs e) 
    { 
     popup.IsOpen = true; 
    } 
} 
+0

謝謝eran!讓我試試看看。 – theOne