anish shres

anish shres

  • NA
  • 3
  • 20k

WPF and Code Behind Issues

Dec 26 2010 10:27 PM
Hello, I am trying to develop a simple gallery like application using C# -  WPF and SQL Server 2005. The issues I am having are:
1. I have designed the Listbox styles and data templates where I plan to show the images. 

The part of a datatemplate code:

<DataTemplate x:Key="GroupDataTemplate" >
            <Grid VerticalAlignment="Center" HorizontalAlignment="Center" Margin="6">

                <!-- Image Template -->
                <Border Padding="4" Background="White" BorderBrush="#22000000" BorderThickness="1">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="{Binding Picture}"  Grid.Row="1" Margin="0" Height="200"/>
                        <Label Name="lblGroup" Content="{Binding Name}" HorizontalAlignment="Center"  Background="#FF13212F" VerticalAlignment="Center" Foreground="White" Height="175" Width="275"/>
                    </StackPanel>
                </Border>
            </Grid>
        </DataTemplate>

Tjhe parf of the ListBox Code:

        <GroupBox Grid.Column="0" Grid.Row="1">
            <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
                <ListBox 
                    ItemTemplate="{StaticResource GroupDataTemplate}" 
                    ItemsSource="{Binding Tables[0]}" 
                    IsSynchronizedWithCurrentItem="True"
                    Name="GroupListBox" 
                    Style="{StaticResource PhotoListBoxStyle}" 
                    Margin="10" 
                    SelectionMode="Extended" 
                    SelectedIndex="0"
                    MouseDoubleClick="GroupListBox_MouseDClick">
                </ListBox>
            </ScrollViewer>
        </GroupBox>


The Code behing in .cs file:

DataSet dtSet = new DataSet(); using (connection = new SqlConnection(connectionString)) {    
                command = new SqlCommand(sql, connection);
                SqlDataAdapter adapter = new SqlDataAdapter();
                connection.Open();
                adapter.SelectCommand = command;
                adapter.Fill(dtSet, "Product_Group");
                GroupListBox.DataContext = dtSet;
    }

First of all, I could not get the picture displayed the picture in the database. Second of all, How can I trap the Primary key the selected ListBoxItem so that I can show the next Window with the details of the Selected ListBox Item.
What I have done here but does not work is:

private void GroupListBox_MouseDClick(object sender, MouseButtonEventArgs e)
        {
            using (connection = new SqlConnection(connectionString))
            {
                connection.Open();
                DataSet ds = new DataSet();
                ListBoxItem lb = (GroupListBox.SelectedItem as ListBoxItem);
                SqlDataAdapter sqa = new SqlDataAdapter("Select Code from Product_Group where name='" + lb.Content + "'", connection);
                sqa.Fill(ds);
                connection.Close();

                this.Close();
            }
        }

I'd be grateful if I could any help. Thanking you in advance. :)

Answers (1)