Silverlight listbox in a list box (show categorized result)
I am new with silverlight and need help.
I have 2 tables: Articles and ArticlesCategory. There is a "one to many" relationship between them (ArticleCategoryId --> Id)

in my XAML I am showing Category names in a listbox, ok there is no problem with getting my data

and I want to show the articles that belongs to each category in another listbox

my XAML : `<ListBox Grid.Column="1" Margin="64,108,0,0" Grid.Row="3" x:Name="ListRecords" ItemsSource="{Binding}" VerticalAlignment="Top" Height="auto" MinHeight="100" BorderBrush="{x:Null}" Background="{x:Null}" Grid.ColumnSpan="2" Width="500" HorizontalAlignment="Left" MinWidth="570" Loaded="ListRecords_Loaded" BorderThickness="0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="sp" Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="10" Height="10" RadiusX="2" RadiusY="2" Fill="#FF4B94B7"   ></Rectangle>
                            <TextBlock Name="Category" Text="{Binding Name}" Margin="5,0,0,0" VerticalAlignment="Stretch" Height="auto"  Foreground="DarkGray" FontSize="12"></TextBlock>
                        </StackPanel>

                        <ListBox Visibility="Visible" x:Name="AltListRecords" ItemsSource="{Binding}" Background="{x:Null}" Grid.ColumnSpan="2" Width="480" HorizontalAlignment="Left" MinWidth="550" BorderBrush="{x:Null}" BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel x:Name="yazi" Orientation="Vertical" Margin="0,10,0,0" >
                                        <TextBlock Name="Title" Text="{Binding Title}" Foreground="#FF4B94B7" FontSize="12" Width="auto" ></TextBlock>
                                        <TextBlock Name="ShortContent" Text="{Binding ShortContent}"  VerticalAlignment="Stretch" Width="500" Height="auto" TextWrapping="Wrap"></TextBlock>
                                     <Button x:Name="BtnDevaminiOku" Click="BtnDevaminiOku_Click" Width="70" Height="15" Foreground="DarkGray"  Background="#FF4B94B7" Opacity="100" FontSize="8" FontWeight="Bold" Margin="400,10,0,0" MaxHeight="15" MaxWidth="70">
                                            <Button.Content>
                                                <Image HorizontalAlignment="Center"  VerticalAlignment="Center" Width="70" Height="15" Source="/DurugonenSilverlight;component/Images/devamButon.JPG" Stretch="Fill" MinHeight="15" MinWidth="70" />
                                            </Button.Content>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>`

my WebService:

     public class Kategori // Kategoriler Entity
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Number { get; set; }
        }

        public class Yazilar // Yazilar için Entity
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string ShortContent { get; set; }
            public string Content { get; set; }
            public int Number { get; set; }
            public int ArticleCategoryId { get; set; }
        }
     [WebMethod]
        public List<Kategori> KategorileriGetir()
        {
            List<Kategori> kategoriler = new List<Kategori>();
            SqlConnection cnn = new SqlConnection("Server=.;Database=mydatabase;uid=xxx;pwd=***");
            SqlCommand cmd = new SqlCommand("Select Id, Name, Number From ArticleCategory Order By Number ", cnn);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                kategoriler.Add(new Kategori() { Id = dr.GetInt32(0), Name = dr.GetString(1) });
            }
            cnn.Close();
            return kategoriler;
        }

        [WebMethod]
        public List<Yazilar> YazilariGetir(int ArticleCategoryId) 
        {
            List<Yazilar> yazilar = new List<Yazilar>();
            SqlConnection cnn = new SqlConnection("Server=.;Database=mydatabase;uid=xxx;pwd=***");
            SqlCommand cmd = new SqlCommand("Select Title, ShortContent, ArticleId From Articles Where ArticleCategoryId=@ArticleCategoryId", cnn);
            cmd.Parameters.AddWithValue("@ArticleCategoryId", ArticleCategoryId);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                yazilar.Add(new Yazilar() { Title = dr.GetString(0), ShortContent = dr.GetString(1) });
            }
            cnn.Close();
            return yazilar;
        }

codebehind:

    ServiceReference1.WebService1SoapClient servis = new ServiceReference1.WebService1SoapClient();
   
        public MainPage()
        {
            InitializeComponent();
          
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            servis.KategorileriGetirCompleted += new EventHandler<ServiceReference1.KategorileriGetirCompletedEventArgs>(servis_KategorileriGetirCompleted);

            servis.YazilariGetirCompleted += new EventHandler<ServiceReference1.YazilariGetirCompletedEventArgs>(servis_YazilariGetirCompleted);
            servis.TumYazilariGetirCompleted += new EventHandler<ServiceReference1.TumYazilariGetirCompletedEventArgs>(servis_TumYazilariGetirCompleted);

        }
        

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            servis.KategorileriGetirAsync();
        }

        void servis_KategorileriGetirCompleted(object sender, ServiceReference1.KategorileriGetirCompletedEventArgs e)
        {

            ListRecords.ItemsSource = e.Result;

            ServiceReference1.Kategori ktgr = new ServiceReference1.Kategori();

            ktgr = ListRecords.Items[ListRecords.Items.Count - 1] as ServiceReference1.Kategori;
            servis.YazilariGetirAsync(ktgr.Id);

        }
        


        void servis_YazilariGetirCompleted(object sender, ServiceReference1.YazilariGetirCompletedEventArgs e)
        {
            StackPanel panelim = FindControl<StackPanel>((UIElement)ListRecords, typeof(StackPanel), "sp");
                        
            ListBox sublist = FindControl<ListBox>((UIElement)panelim, typeof(ListBox), "AltListRecords");

            sublist.ItemsSource = e.Result;

        }



        public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
        {

            if (parent == null) return null;

            if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
            {
                return (T)parent;
            }
            T result = null;
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);

                if (FindControl<T>(child, targetType, ControlName) != null)
                {
                    result = FindControl<T>(child, targetType, ControlName);
                    break;
                }
            }
            return result;
        }

I want the result like this :

X Category

   Article 1 (title and shortcontent)

   Article 2 (title and shortcontent)

   Article 3 (title and shortcontent)

Y Category

  Article 4 (title and shortcontent)

  Article 5 (title and shortcontent)

Z Category

  Article 6 (title and shortcontent)

  Article 7 (title and shortcontent)

  Article 8 (title and shortcontent)

...

Thanks for your reply.




Silverlight listbox in a list box (show categorized result)
===

I am new with silverlight and need help.
I have 2 tables: Articles and ArticlesCategory. There is a "one to many" relationship between them (ArticleCategoryId --> Id)

in my XAML I am showing Category names in a listbox, ok there is no problem with getting my data

and I want to show the articles that belongs to each category in another listbox

my XAML : `<ListBox Grid.Column="1" Margin="64,108,0,0" Grid.Row="3" x:Name="ListRecords" ItemsSource="{Binding}" VerticalAlignment="Top" Height="auto" MinHeight="100" BorderBrush="{x:Null}" Background="{x:Null}" Grid.ColumnSpan="2" Width="500" HorizontalAlignment="Left" MinWidth="570" Loaded="ListRecords_Loaded" BorderThickness="0" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel x:Name="sp" Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <Rectangle Width="10" Height="10" RadiusX="2" RadiusY="2" Fill="#FF4B94B7"   ></Rectangle>
                            <TextBlock Name="Category" Text="{Binding Name}" Margin="5,0,0,0" VerticalAlignment="Stretch" Height="auto"  Foreground="DarkGray" FontSize="12"></TextBlock>
                        </StackPanel>

                        <ListBox Visibility="Visible" x:Name="AltListRecords" ItemsSource="{Binding}" Background="{x:Null}" Grid.ColumnSpan="2" Width="480" HorizontalAlignment="Left" MinWidth="550" BorderBrush="{x:Null}" BorderThickness="0">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel x:Name="yazi" Orientation="Vertical" Margin="0,10,0,0" >
                                        <TextBlock Name="Title" Text="{Binding Title}" Foreground="#FF4B94B7" FontSize="12" Width="auto" ></TextBlock>
                                        <TextBlock Name="ShortContent" Text="{Binding ShortContent}"  VerticalAlignment="Stretch" Width="500" Height="auto" TextWrapping="Wrap"></TextBlock>
                                     <Button x:Name="BtnDevaminiOku" Click="BtnDevaminiOku_Click" Width="70" Height="15" Foreground="DarkGray"  Background="#FF4B94B7" Opacity="100" FontSize="8" FontWeight="Bold" Margin="400,10,0,0" MaxHeight="15" MaxWidth="70">
                                            <Button.Content>
                                                <Image HorizontalAlignment="Center"  VerticalAlignment="Center" Width="70" Height="15" Source="/DurugonenSilverlight;component/Images/devamButon.JPG" Stretch="Fill" MinHeight="15" MinWidth="70" />
                                            </Button.Content>
                                        </Button>
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>`

my WebService:

     public class Kategori // Kategoriler Entity
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int Number { get; set; }
        }

        public class Yazilar // Yazilar için Entity
        {
            public int Id { get; set; }
            public string Title { get; set; }
            public string ShortContent { get; set; }
            public string Content { get; set; }
            public int Number { get; set; }
            public int ArticleCategoryId { get; set; }
        }
     [WebMethod]
        public List<Kategori> KategorileriGetir()
        {
            List<Kategori> kategoriler = new List<Kategori>();
            SqlConnection cnn = new SqlConnection("Server=.;Database=mydatabase;uid=xxx;pwd=***");
            SqlCommand cmd = new SqlCommand("Select Id, Name, Number From ArticleCategory Order By Number ", cnn);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                kategoriler.Add(new Kategori() { Id = dr.GetInt32(0), Name = dr.GetString(1) });
            }
            cnn.Close();
            return kategoriler;
        }

        [WebMethod]
        public List<Yazilar> YazilariGetir(int ArticleCategoryId) 
        {
            List<Yazilar> yazilar = new List<Yazilar>();
            SqlConnection cnn = new SqlConnection("Server=.;Database=mydatabase;uid=xxx;pwd=***");
            SqlCommand cmd = new SqlCommand("Select Title, ShortContent, ArticleId From Articles Where ArticleCategoryId=@ArticleCategoryId", cnn);
            cmd.Parameters.AddWithValue("@ArticleCategoryId", ArticleCategoryId);
            cnn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                yazilar.Add(new Yazilar() { Title = dr.GetString(0), ShortContent = dr.GetString(1) });
            }
            cnn.Close();
            return yazilar;
        }

codebehind:

    ServiceReference1.WebService1SoapClient servis = new ServiceReference1.WebService1SoapClient();
   
        public MainPage()
        {
            InitializeComponent();
          
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);

            servis.KategorileriGetirCompleted += new EventHandler<ServiceReference1.KategorileriGetirCompletedEventArgs>(servis_KategorileriGetirCompleted);

            servis.YazilariGetirCompleted += new EventHandler<ServiceReference1.YazilariGetirCompletedEventArgs>(servis_YazilariGetirCompleted);
            servis.TumYazilariGetirCompleted += new EventHandler<ServiceReference1.TumYazilariGetirCompletedEventArgs>(servis_TumYazilariGetirCompleted);

        }
        

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            servis.KategorileriGetirAsync();
        }

        void servis_KategorileriGetirCompleted(object sender, ServiceReference1.KategorileriGetirCompletedEventArgs e)
        {

            ListRecords.ItemsSource = e.Result;

            ServiceReference1.Kategori ktgr = new ServiceReference1.Kategori();

            ktgr = ListRecords.Items[ListRecords.Items.Count - 1] as ServiceReference1.Kategori;
            servis.YazilariGetirAsync(ktgr.Id);

        }
        


        void servis_YazilariGetirCompleted(object sender, ServiceReference1.YazilariGetirCompletedEventArgs e)
        {
            StackPanel panelim = FindControl<StackPanel>((UIElement)ListRecords, typeof(StackPanel), "sp");
                        
            ListBox sublist = FindControl<ListBox>((UIElement)panelim, typeof(ListBox), "AltListRecords");

            sublist.ItemsSource = e.Result;

        }



        public T FindControl<T>(UIElement parent, Type targetType, string ControlName) where T : FrameworkElement
        {

            if (parent == null) return null;

            if (parent.GetType() == targetType && ((T)parent).Name == ControlName)
            {
                return (T)parent;
            }
            T result = null;
            int count = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < count; i++)
            {
                UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);

                if (FindControl<T>(child, targetType, ControlName) != null)
                {
                    result = FindControl<T>(child, targetType, ControlName);
                    break;
                }
            }
            return result;
        }

I want the result like this :

X Category

   Article 1 (title and shortcontent)

   Article 2 (title and shortcontent)

   Article 3 (title and shortcontent)

Y Category

  Article 4 (title and shortcontent)

  Article 5 (title and shortcontent)

Z Category

  Article 6 (title and shortcontent)

  Article 7 (title and shortcontent)

  Article 8 (title and shortcontent)

...

Thanks for your reply.



