Nested listbox with date from to difrent tabbels.

Aug 4, 2010 at 10:35 AM



I'm trying to make a wpf app with a vertical list-box that contains a list with items that has a horizontal list-box white detailed sub items from a database source.

I have a lot of it working accept the last part of how to pas a value of a text-box or record-id from the firs list-box  back to data source for the detailed list-box?

this is the xaml code.

<Window x:Class="MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:system="clr-namespace:System;assembly=mscorlib"
  xmlns:local="clr-namespace:WpfApplication1.Dataprovider"
  Title="MainWindow" Height="395" Width="835">

  <Window.Resources>
    <CollectionViewSource x:Key="ProductCategorySource" />
    <CollectionViewSource x:Key="ProductSubcategory" />
    

  </Window.Resources>

  <Grid>
    <ListBox ItemsSource="{Binding Source={StaticResource ProductCategorySource}}" Height="320" HorizontalAlignment="Left" Margin="10,15,0,0" Name="ListBoxMasterView" VerticalAlignment="Top" Width="758" >
      <ListBox.Resources >
        <ObjectDataProvider x:Key="ProductSubcategorytest" ObjectType="{x:Type local:Dataproviderclass}" MethodName="ProductSubcategorylist">
          <ObjectDataProvider.MethodParameters>
            <system:String>1</system:String>
          </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
      </ListBox.Resources>
      <ItemsControl.ItemTemplate>
        <DataTemplate >
          <Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="5">

            <Grid Height="auto" Width="auto" >
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="80" />
                <ColumnDefinition Width="auto" />
              </Grid.ColumnDefinitions>
              <StackPanel Orientation="Vertical" Grid.Column="0" >
                <TextBlock Name="TextBoxProductCategoryKey" Text="{Binding Path=ProductCategoryKey}" />
                <TextBlock Text="{Binding Path=EnglishProductCategoryName}"/>
              </StackPanel>

              <ListBox ItemsSource="{Binding Source={StaticResource ProductSubcategorytest}}" Height="auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="ListBoxDetailsView" VerticalAlignment="Top" Width="auto" Grid.Column="1" IsSynchronizedWithCurrentItem="True" >
                <ListBox.Style>
                  <Style TargetType="{x:Type ListBox}">

                    <Setter Property="ItemsPanel">
                      <Setter.Value>
                        <ItemsPanelTemplate>
                          <WrapPanel />
                        </ItemsPanelTemplate>
                      </Setter.Value>
                    </Setter>

                  </Style>
                </ListBox.Style>
                <ItemsControl.ItemTemplate>
                  <DataTemplate >
                    <Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="5">
                      <StackPanel Orientation="Vertical" >
                        <TextBlock Text="{Binding Path=ProductCategoryKey}"/>
                        <TextBlock Text="{Binding Path=EnglishProductSubcategoryName}"/>
                        <TextBlock Text="{Binding Path=SpanishProductSubcategoryName}"/>
                      </StackPanel>
                    </Border>
                  </DataTemplate>
                </ItemsControl.ItemTemplate>
              </ListBox>




            </Grid>
          </Border>

        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ListBox>

  </Grid>
</Window>

The code behind.

Class MainWindow 
  Private db As New AdventureWorksDW2008R2Entities1
  Private DimProductdata As IEnumerable(Of DimProductCategory)
  Private DimProductdatasource As CollectionViewSource
  Private DimProductSubcategorydatasource As CollectionViewSource

  Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
    'Me.DimProductdata = db.DimProductCategory

    Dim ProductCategorylist = From c In db.DimProductCategory
    Me.DimProductdatasource = CType(Me.FindResource("ProductCategorySource"), CollectionViewSource)
    DimProductdatasource.Source = ProductCategorylist

    Dim DimProductSubcategorylist = From c In db.DimProductSubcategory
    Me.DimProductSubcategorydatasource = CType(Me.FindResource("ProductSubcategory"), CollectionViewSource)
    DimProductSubcategorydatasource.Source = DimProductSubcategorylist

    'DimProductdatasource.Source = Me.DimProductdata
  End Sub
End Class

and the code from Public Class Dataproviderclass.

Namespace Dataprovider
  Public Class Dataproviderclass

    Private db As New AdventureWorksDW2008R2Entities1
    Private DimProductSubcategorydatasource As CollectionViewSource

    Public Function ProductSubcategorylist(ByVal strProductCategoryKey As String)
      If strProductCategoryKey = String.Empty Then
        Return Nothing
      Else
        Dim DimProductSubcategorylist = From c In db.DimProductSubcategory Where c.ProductCategoryKey = strProductCategoryKey
        'Me.DimProductSubcategorydatasource = CType(Me.FindResource("ProductSubcategory"), CollectionViewSource)
        'DimProductSubcategorydatasource.Source = DimProductSubcategorylist
        Return DimProductSubcategorylist
      End If

    End Function
  End Class
End Namespace

If there is a smarter way of doing this i like to no.

 

Thanks

 

Perwin.

Aug 6, 2010 at 2:53 PM

Hi Perwin,

Can u plz elaborate on ur issue? I see that u have no textbox in the code anywhere. also what is record id? which class has this property?

Thx

Vinit

Aug 9, 2010 at 7:28 AM

Hi Vinit

 

I have textblock's instead of text-box in the xaml code.

The record-id is the record-id from The first database table (DimProductdata) the second table is ProductSubcategorydata Both of those tables come from the AdventureWorksDW2008R2 database not from a class.

I already moved the objectdataProvider to the grid inside the listbox and i know it gets trigger for every row in the main listbox.

I like to know how i get the value from   <TextBlock  Name="TextBoxProductCategoryKey" Text="{Binding Path=ProductCategoryKey}" /> en pas it to the <ObjectDataProvider.MethodParameters>

 

New Xaml code

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:WpfApplication1.Dataprovider"
    Title="MainWindow" Height="395" Width="835">

    <Window.Resources>
        <CollectionViewSource x:Key="ProductCategorySource" />
        <CollectionViewSource x:Key="ProductSubcategory" />
        

    </Window.Resources>

    <Grid>
        <ListBox ItemsSource="{Binding Source={StaticResource ProductCategorySource}}"  Height="320" HorizontalAlignment="Left" Margin="10,15,0,0" Name="ListBoxMasterView" VerticalAlignment="Top" Width="758" >
              <ItemsControl.ItemTemplate>
                <DataTemplate >
                    <Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="5">

                        <Grid Height="auto" Width="auto" >
                            <Grid.Resources >
                                <ObjectDataProvider x:Key="ProductSubcategorytest" ObjectType="{x:Type local:Dataproviderclass}" MethodName="ProductSubcategorylist">
                                    <ObjectDataProvider.MethodParameters>
                                      <system:String>2</system:String>
                                    </ObjectDataProvider.MethodParameters>
                                </ObjectDataProvider>
                            </Grid.Resources>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="80" />
                                <ColumnDefinition Width="auto" />
                            </Grid.ColumnDefinitions>
                            <StackPanel Orientation="Vertical" Grid.Column="0" >
                                <TextBlock  Name="TextBoxProductCategoryKey" Text="{Binding Path=ProductCategoryKey}" />
                                <TextBlock Text="{Binding Path=EnglishProductCategoryName}"/>
                            </StackPanel>

                            <ListBox ItemsSource="{Binding Source={StaticResource ProductSubcategorytest}}" Height="auto" HorizontalAlignment="Left" Margin="10,0,0,0" Name="ListBoxDetailsView" VerticalAlignment="Top" Width="auto" Grid.Column="1" IsSynchronizedWithCurrentItem="True" >
                                
                                <ListBox.Style>
                                    <Style TargetType="{x:Type ListBox}">

                                        <Setter Property="ItemsPanel">
                                            <Setter.Value>
                                                <ItemsPanelTemplate>
                                                    <WrapPanel />
                                                </ItemsPanelTemplate>
                                            </Setter.Value>
                                        </Setter>

                                    </Style>
                                </ListBox.Style>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate >
                                        <Border BorderBrush="Blue" Margin="3" Padding="3" BorderThickness="2" CornerRadius="5">
                                            <StackPanel Orientation="Vertical" >

                                                <TextBlock Text="{Binding Path=ProductCategoryKey}"/>
                                                <TextBlock Text="{Binding Path=EnglishProductSubcategoryName}"/>
                                                <TextBlock Text="{Binding Path=SpanishProductSubcategoryName}"/>
                                            </StackPanel>
                                        </Border>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                               
                            </ListBox>




                        </Grid>
                    </Border>

                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox>

    </Grid>
</Window>


Thanks

Perwin