tags:

views:

48

answers:

3

hi there i am developing a wpf app. i have a database like this. i have three columns(id,name,profession).listbox shows name column. When the user clicks the item in listbox, i wanna show his/her profession in textblock. listbox works well. i have bounded it to a dataview. but how can i show his/her profession in textblock?

private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)

{

What can i write here?

    }

thanks in advance.

A: 

You should proably just bind the text attribute of the TextBlock to the selected item of the list box. 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>
Muad'Dib
no not like that! listbox shows names. but i wanna show professions in textblock. for an example;listbox shows David Beckham. When the user clicks David Bekcham. tetxblock must show a footballer.
Yes, like this. All you have to do is tell it the path to the item.{Binding ElementName="myListBox" Path="SelectedItem.Profession"}
Muad'Dib
give me a chunk of your xaml, i will show you.
Muad'Dib
A: 

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 { /// /// Interaction logic for Window1.xaml /// 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);

    }







}

}

            <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>

thanks very much:
A: 

Thank you very much. I made it as you said. and It worked . I am so glad . thank you Chris. Best Regards, Lilly