2009-08-17 8 views
1

Je développe une application wpf. J'ai une base de données comme celle-ci. J'ai trois colonnes (id, nom, profession) .listbox montre la colonne de nom. Lorsque l'utilisateur clique sur l'élément dans la liste, je veux montrer sa profession dans le bloc de texte. listbox fonctionne bien. Je l'ai lié à une vue de données. mais comment puis-je montrer sa profession dans le bloc de texte?ListBox Article sélectionné

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e) 

{ 

//What can I write here? 

} 

Répondre

3

Vous devriez probablement juste lier l'attribut de texte du TextBlock à l'élément sélectionné de la zone de liste. Check this article at MSDN.

<StackPanel> 
    <TextBlock Width="248" Height="24" Text="Colors:" 
     TextWrapping="Wrap"/> 
    <ListBox x:Name="lbColor" Width="248" Height="56"> 
     <ListBoxItem Content="Blue"/> 
     <ListBoxItem Content="Green"/> 
     <ListBoxItem Content="Yellow"/> 
     <ListBoxItem Content="Red"/> 
     <ListBoxItem Content="Purple"/> 
     <ListBoxItem Content="Orange"/> 
    </ListBox> 
    <TextBlock Width="248" Height="24" Text="You selected color:" /> 
    <TextBlock Width="248" Height="24"> 
     <TextBlock.Text> 
      <Binding ElementName="lbColor" Path="SelectedItem.Content"/> 
     </TextBlock.Text> 
    </TextBlock> 
</StackPanel> 
+0

ne pas comme ça! listbox affiche les noms. mais je veux montrer des professions dans le bloc de texte. pour un exemple; listbox montre David Beckham. Lorsque l'utilisateur clique sur David Bekcham. tetxblock doit montrer un footballeur. –

+0

Oui, comme ça. Tout ce que vous avez à faire est de lui dire le chemin de l'article. {Binding ElementName = "myListBox" Path = "SelectedItem.Profession"} –

+0

donnez-moi un morceau de votre xaml, je vais vous montrer. –

0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 
using System.Data; 
using System.Data.OleDb; 

namespace WpfApplication2 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public DataView view; 
     public DataSet ds; 
     public Window1() 
     { 

      InitializeComponent(); 
      BindData(); 


     } 


     public void BindData() { 

      OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\Information.mdb;Persist Security Info=True"); 
      con.Open(); 
      string sql = "Select * from Dictionary"; 
      OleDbCommand cmd = new OleDbCommand(sql, con); 
      OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
      DataSet ds = new DataSet(); 
      try 
      { 

       da.Fill(ds, "Info"); 
       view = ds.Tables[0].DefaultView; 
       listBox1.ItemsSource = view; 


      } 
      catch (Exception ex) 
      { 




      }} 

     private void textBox1_TextChanged(object sender, TextChangedEventArgs e) 
      { 

       view.RowFilter = string.Format("Name Like '{0}%'", textBox1.Text); 

     } 







    } 
} 




<Window x:Class="WpfApplication2.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="490"> 
    <Grid> 
     <ListBox Margin="18,77,0,83" Name="listBox1" ItemsSource="{Binding Path=Name}" HorizontalAlignment="Left" Width="213" SelectionChanged="listBox1_SelectionChanged" > 
      <ListBox.ItemTemplate> 

       <DataTemplate> 

        <TextBlock Text="{Binding Path=Name}"> </TextBlock> 
       </DataTemplate> 



      </ListBox.ItemTemplate> 
     </ListBox> 

     <TextBox Height="23" Margin="18,40,0,0" Name="textBox1" VerticalAlignment="Top" TextChanged="textBox1_TextChanged" HorizontalAlignment="Left" Width="213" /> 
     <TextBlock HorizontalAlignment="Right" Margin="0,77,42,83" Name="textBlock1" Width="185" Background="#FFE6C6C6" /><Label Height="28" HorizontalAlignment="Right" Margin="0,39,76,0" Name="label1" VerticalAlignment="Top" Width="140">Label</Label> 
    </Grid> 
</Window> 
+0

merci beaucoup: –

Questions connexes