2013-06-30 24 views
0

我想在我的數據網格中添加搜索面板,代碼中沒有錯誤,但是如果我輸入任何值,它不會顯示任何輸出。你能回答我的問題嗎?我的代碼是在這裏如下:搜索面板錯誤

 <Window x:Class="WpfApplication3.MainWindow" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" 
      dx:ThemeManager.ThemeName="MetropolisDark" 
      Title="MainWindow" Height="350" Width="525" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      ResizeMode="CanMinimize" mc:Ignorable="d" Loaded="Window_Loaded" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors"> 


      <Grid Background="#FF333333" > 
      <Grid.RowDefinitions> 
       <RowDefinition Height="Auto" /> 
       <RowDefinition Height="*"/> 
       <RowDefinition Height="Auto"/> 
      </Grid.RowDefinitions> 

      <TextBlock Grid.Row="1" Height="28" HorizontalAlignment="Left" Margin="41,12,0,0" x:Name="textBlock1" Text="Type here" VerticalAlignment="Top" Width="71" /> 
      <TextBox Grid.Row="1" HorizontalAlignment="Left" Margin="139,12,0,246" x:Name="textBox1" Width="233" TextWrapping="NoWrap" TextChanged="textBox1_TextChanged" /> 
      <ListBox Grid.Row="1" Background="LightYellow" Visibility="Collapsed" Height="33" HorizontalAlignment="Left" Margin="220,45,0,0" Name="listBox1" VerticalAlignment="Top" Width="202" /> 
     </Grid> 
</Window> 
 public partial class MainWindow : Window 
    { 
     //object used for update data 
     DataClasses1DataContext objContext = new DataClasses1DataContext(); 
     //object used to update selected row data 
     Assignment student = null; 
     IEnumerable eventt_grp1; 
     public MainWindow() 
     { 
      InitializeComponent(); 
       //Database context 
       //StudentDBDataContext objContext = new StudentDBDataContext(); 

       //Linq to SQL: this is like sql select query 
       //std is table alias 
       //objContext.StudentDetails is table from data is seleted 
       //var result: behaves like DataSet/DataTable 
       List<Assignment> a = new List<Assignment>(); 

       eventt_grp1 = a.Select(r => new { r.assignment_title }).ToList(); 

       textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged); 

       //Show message if it has rows 


     } 

     private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
     { 

    string typedstring= textBox1.Text; 
List<string> autolist= new List<string>(); 
foreach(string b in eventt_grp1) 
{ 
if(!string.IsNullOrEmpty(textBox1.Text)) 
{ 
if(b.StartsWith(typedstring)) 
{ 
autolist.Add(b); 
} 
} 
} 
if(autolist.Count>0) 
{ 
    listBox1.ItemsSource = autolist; 
    listBox1.Visibility = Visibility.Visible; 

} 
else if (textBox1.Text.Equals ("")) 
{ 

    listBox1.Visibility = Visibility.Collapsed; 
    listBox1.ItemsSource = null; 
} 

else 
{ 

    listBox1.Visibility = Visibility.Collapsed; 
    listBox1.ItemsSource = null; 
} 


     } 

     private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

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

i 我重視的是形象呢!謝謝 !

+3

請購買您使用的軟件。 –

回答

0

我建議你調試流程。 autolist可能是空的。還可以使用它來更新數據源:

listBox1.DataSource = null; 

listBox1.DataSource = myList; 

希望它可以幫助

編輯: 提示 - >格式的代碼!