none
Listview with expander

    Question

  • I am trying to create a table where a set of rows could be expanded/collapsed. I believe I should be using the ListView control (gridview mode) and the Expander control. A step by step guide on how to do it would be extremely helpful.

    Things I am particularly stuck at are:
    1. How to add the expander control to the listview
    2. How to handle the expand/collapse messages
    3. How to do thigs in the code as I expect rows to be dynamically added/removed from the table.

    Thanks in advance!
    Tuesday, November 24, 2009 4:36 AM

Answers

  • Do not use

    col.DisplayMemberBinding = new Binding("ProvName");
    

    Here is your required code.


    col = new GridViewColumn();
    col.Width = 75;
    col.Header = "ContentType";
    
    DataTemplate dt = new DataTemplate();
    FrameworkElementFactory expander = new FrameworkElementFactory(typeof(Expander));
    
    Binding binding = new Binding();
    binding.Path = new PropertyPath("ContentType");
    
    FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
    text.SetBinding(TextBlock.TextProperty, binding);
    text.SetValue(ForegroundProperty, Brushes.Green);
    text.SetValue(FontSizeProperty, Convert.ToDouble(14));
    expander.AppendChild(text);
    
    dt.VisualTree = expander;
    col.CellTemplate = dt;
    
    grid.Columns.Add(col);
    


    Please let us know if it still doesn't work.

    Zeeshan Amjad
    http://zamjad.wordpress.com/
    • Proposed as answer by Zeeshan Amjad Friday, November 27, 2009 4:28 AM
    • Marked as answer by Jim Zhou - MSFT Tuesday, December 01, 2009 8:27 AM
    Friday, November 27, 2009 4:28 AM
  • Hi Xl3k,

    I recommend you not to do all the logic using pure code if there is not special reason, the following is a ready-to-run example of using the grouging, please check it out.

    Code snippet:

     

    <Window.Resources>

            <!--xml data-->

            <XmlDataProvider x:Key="MyData" XPath="/Info/Item">

                <x:XData>

                    <Info xmlns="">

                        <Item ID="12345" Name="Book 1" Price="$32.05"

                    Author="Author A" Catalog="Business"/>

                        <Item ID="13590" Name="Book 2" Price="$10.00"

                    Author="Author B" Catalog="Language"/>

                        <Item ID="24678" Name="Book 3" Price="$9.00"

                    Author="Author C" Catalog="Language"/>

                        <Item ID="65432" Name="Book 4" Price="$8.50"

                    Author="Author D" Catalog="Business"/>

                        <Item ID="11233" Name="Book 5" Price="$19.00"

                    Author="Author E" Catalog="Health"/>

                        <Item ID="94837" Name="Book 6" Price="$8.50"

                    Author="Author F" Catalog="Language"/>

                    </Info>

                </x:XData>

            </XmlDataProvider>

            <!--CollectionViewSource-->

            <CollectionViewSource x:Key='src'

                              Source="{Binding Source={StaticResource MyData}}">

                <CollectionViewSource.GroupDescriptions>

                    <PropertyGroupDescription PropertyName="@Catalog" />

                </CollectionViewSource.GroupDescriptions>

            </CollectionViewSource>

        </Window.Resources>

        <!--Content-->

        <ListView ItemsSource='{Binding Source={StaticResource src}}'

                BorderThickness="0">

            <ListView.GroupStyle>

                <!--ObservableCollection<GroupStyle>-->

                <GroupStyle>

                    <GroupStyle.ContainerStyle>

                        <Style TargetType="{x:Type GroupItem}">

                            <Setter Property="Margin" Value="0,0,0,5"/>

                            <Setter Property="Template">

                                <Setter.Value>

                                    <ControlTemplate TargetType="{x:Type GroupItem}">

                                        <Expander IsExpanded="True"

                                                  BorderBrush="#FFA4B97F"

                                                  BorderThickness="0,0,0,1">

                                            <Expander.Header>

                                                <DockPanel>

                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"

                                       Margin="5,0,0,0" Width="100"/>

                                                    <TextBlock FontWeight="Bold"

                                       Text="{Binding Path=ItemCount}"/>

                                                </DockPanel>

                                            </Expander.Header>

                                            <Expander.Content>

                                                <ItemsPresenter />

                                            </Expander.Content>

                                        </Expander>

                                    </ControlTemplate>

                                </Setter.Value>

                            </Setter>

                        </Style>

                    </GroupStyle.ContainerStyle>

                </GroupStyle>

            </ListView.GroupStyle>

            <ListView.View>

                <GridView>

                    <GridViewColumn Header="ID"

                            DisplayMemberBinding="{Binding XPath=@ID}"

                            Width="100" />

                    <GridViewColumn Header="Name"

                            DisplayMemberBinding="{Binding XPath=@Name}"

                            Width="140" />m

                    <GridViewColumn Header="Price"

                            DisplayMemberBinding="{Binding XPath=@Price}"

                            Width="80" />

                    <GridViewColumn Header="Author"

                            DisplayMemberBinding="{Binding XPath=@Author}"

                            Width="80" />

                </GridView>

            </ListView.View>

        </ListView>

    For creating group style in code behind, you can see this link:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9889a0b7-dc9c-4f80-b92d-0a2e761e82b6

    Any issues with this, please feel free to feed back.
    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Tuesday, December 01, 2009 8:34 AM

All replies

  • Ok. I have the Grid, I have the data, I have the grouping and surely I have the expander. But my groups don't collapse when I click the expander. What am I missing!

    Here goes my xaml

    <Page x:Class="ProgramDetailsWindow.Page1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
        xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework" 
       
        Title="Page1" Width="484" Loaded="Page_Loaded" Height="1000">
    
        <Page.Resources>    
    
            <CollectionViewSource x:Key="group" >
            </CollectionViewSource>
    
            <DataTemplate x:Key="categoryTemplate">
                <Expander Header="{Binding Path=Name}" IsExpanded="True" BorderBrush="DarkBlue"  Visibility="{Binding Path=Visibility, RelativeSource={RelativeSource AncestorType={x:Type GridView}}}" Grid.IsSharedSizeScope="False">
                </Expander>                
            </DataTemplate>
    
        </Page.Resources>
    
    
    
    
        <Canvas Height="830" Name="canvas1" Width="385">        
            <ListView Canvas.Left="12" Canvas.Top="50" Height="370" Name="lv" Width="361">     
                <ListView.View>                
                    <GridView x:Name="grid">                   
                    </GridView>   
                </ListView.View>
            </ListView>
        </Canvas>
    </Page>

    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 Microsoft.Windows.Controls;
    using System.Collections.ObjectModel;
    using System.Collections;
    using System.Data;
    using System.Globalization;
    using System.ComponentModel;
    
    
    namespace ProgramDetailsWindow
    {
        
        public class ProgData
        {
    
            public String Name { get; set; }
            public String ProvName { get; set; }
    
            public ProgramStatusType Status { get; set; }
            public ProgramContentType ContentType { get; set; }
    
          
        }
        
    
        
      
       
    
        
        /// <summary>
        /// Interaction logic for Page1.xaml
        /// </summary>
        public partial class Page1 : Page
        {
            
    
           ObservableCollection<ProgData> ProgCollection = new ObservableCollection<ProgData>();
            int No_of_Elements = 20;
    
       
            DataTable table = new DataTable();
    
    
            CollectionView cv;
    
    
    
            public Page1()
            {
                InitializeComponent();
    
                cv = new CollectionView(ProgCollection);
                
                Random rnd = new Random();
    
              
    
           
    
                for (int i = 0; i < No_of_Elements; ++i)
                { 
                    ProgData data = new ProgData();
                 
                    data.Name = "Program Name " + i;
                    data.ProvName = "Provider Name " + i;
                    //Random rnd = new Random();
                    data.Status = (ProgramStatusType)rnd.Next(2);
                    data.ContentType = (ProgramContentType)rnd.Next(4);
                    ProgCollection.Add(data);
                }
    
               
                
            }
    
            private void Page_Loaded(object sender, RoutedEventArgs e)
            {
                Binding bind = new Binding();
    
                CollectionViewSource cvs = (CollectionViewSource)this.Resources["group"];
                cvs.SortDescriptions.Add(new SortDescription("ContentType", ListSortDirection.Ascending));
                cvs.GroupDescriptions.Add(new PropertyGroupDescription("ContentType"));
                cvs.Source = ProgCollection;//assign collection object here
                lv.DataContext = cvs;
                lv.SetBinding(ListView.ItemsSourceProperty, bind);
                GroupStyle gs = new GroupStyle();
                DataTemplate dt = (DataTemplate)this.FindResource("categoryTemplate");
               
                gs.HeaderTemplate = dt;
              
                Style style = new Style(typeof(GroupItem));
    
                //setter 1
                Setter setter = new Setter();
                setter.Property = GroupItem.MarginProperty;
                setter.Value = new Thickness(0, 0, 0, 5);
                style.Setters.Add(setter);
    
               
    
                gs.ContainerStyle = style;
    
    
                lv.GroupStyle.Add(gs);
    
    
                
    
                GridViewColumn col = new GridViewColumn();
                col.Width = 100;
                col.Header = "Program name";
                col.DisplayMemberBinding = new Binding("Name");
                grid.Columns.Add(col);
    
    
                col = new GridViewColumn();
                col.Width = 100;
                col.Header = "Program provider";
                col.DisplayMemberBinding = new Binding("ProvName");
                grid.Columns.Add(col);
    
                //col = new GridViewColumn();
                //col.Width = 75;
                //col.Header = "ContentType";
                //col.DisplayMemberBinding = new Binding("ContentType");
                //grid.Columns.Add(col);
    
                col = new GridViewColumn();
                col.Width = 100;
                col.Header = "Status";
                col.DisplayMemberBinding = new Binding("Status");
                grid.Columns.Add(col);
            }
    
    
        }
    }
    
    • Edited by xl3k Wednesday, November 25, 2009 12:32 PM formatting ...
    Wednesday, November 25, 2009 12:29 PM
  • I already wrote one example about this please take a look at

    http://zamjad.wordpress.com/2009/11/19/using-expender-control-inside-list-view/

    In short you are suppose to implement the data tempplate and set it as a property of CellTemplate of GridViewColumn. Here is a small piece of XAML code to do this.

    <GridViewColumn.CellTemplate>
    	<DataTemplate>
    		<Expander ExpandDirection="Right">
    			<TextBlock Foreground="Green" FontSize="14" Text="{Binding NickName}"/>
    		</Expander>
    	</DataTemplate>
    </GridViewColumn.CellTemplate>
    

    Regards
    Zeeshan Amjad
    http://zamjad.wordpress.com/

    • Proposed as answer by Zeeshan Amjad Thursday, November 26, 2009 3:43 AM
    Wednesday, November 25, 2009 4:25 PM
  • Hi Zeeshan, thanks for your response. Went through your sample too and it was great! However all your columndefinitions are in XAML while mine are in code. I am not sure how I achieve something like the above snippet in code. Could you help out a bit more, please.
    Thursday, November 26, 2009 5:20 AM
  • Here is a piece of C# code to do almost the same thing.

                GridView gv = list.View as GridView;
    
                if (gv != null)
                {
                    GridViewColumn gvc = gv.Columns[3];
    
                    DataTemplate dt = new DataTemplate();
                    FrameworkElementFactory expander = new FrameworkElementFactory(typeof(Expander));
    
                    Binding binding = new Binding();
                    binding.Path = new PropertyPath("NickName");
    
                    FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
                    text.SetBinding(TextBlock.TextProperty, binding);
                    text.SetValue(ForegroundProperty, Brushes.Green);
                    text.SetValue(FontSizeProperty, Convert.ToDouble(14));
                    expander.AppendChild(text);
    
                    dt.VisualTree = expander;
                    gvc.CellTemplate = dt;
                }
    

    But i already assumed that you have at least define the columns in the XAML and only going to define the data template and data binding in C#. Here is is XAML code for defining the columns without any binding or data template.

            <ListView ItemContainerStyle="{StaticResource myStyle}" Name="list" Margin="5" HorizontalContentAlignment="Stretch">
                <ListView.View>                
                    <GridView>
                        <GridView.ColumnHeaderTemplate>
                            <DataTemplate>
                                <Border BorderBrush="Brown" BorderThickness="2" CornerRadius="5">
                                    <Border.Background>
                                        <LinearGradientBrush>
                                            <GradientStop Offset="0" Color="Wheat"/>
                                            <GradientStop Offset="1" Color="LightCoral"/>
                                        </LinearGradientBrush>
                                    </Border.Background>
                                    <TextBlock Foreground="Blue" FontSize="14" FontWeight="Bold" Margin="5" Text="{Binding}" Width="Auto"/>    
                                </Border>                            
                            </DataTemplate>
                        </GridView.ColumnHeaderTemplate>
                        <GridViewColumn Width="Auto" Header="Visited">
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding Visited}"/>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                        <GridViewColumn Width="Auto" Header="State Name" DisplayMemberBinding="{Binding Path=Name}"/>
                        <GridViewColumn Width="Auto" Header="State Capital" DisplayMemberBinding="{Binding Path=Capital}"/>
                        <GridViewColumn Width="Auto" Header="State Nick Name">
                        </GridViewColumn>
                    </GridView>
                </ListView.View>
            </ListView>
    

    Please let us know if you also want to define columns defination in Code or do you want to do everything in Code and nothing in XAML.

    Zeeshan Amjad
    http://zamjad.wordpress.com/



    Thursday, November 26, 2009 6:54 AM
  • Hi Zeeshan, yes I want to define the columns entirely in code.

    Heres how things are like:

    My data object is as below:
    public class ProgData
        {
    
            public String Name { get; set; }
            public String ProvName { get; set; }
    
            public ProgramStatusType Status { get; set; }
            public ProgramContentType ContentType { get; set; }
    
          
        }

    I make a collection like
     ObservableCollection<ProgData> ProgCollection = new ObservableCollection<ProgData>();
    I populate the collection class with some data.
    Next comes my column creation and grouping part, which is like below.
    I create a collectionviewsource, update its grouping and sorting  properties and attach it as a datasource for the grid, do the binding etc!
    CollectionViewSource cvs = new CollectionViewSource() ;
    cvs.Source = ProgCollection;//assign collection object here
    cvs.SortDescriptions.Add(new SortDescription("ContentType", ListSortDirection.Ascending));
    cvs.GroupDescriptions.Add(new PropertyGroupDescription("ContentType"));
    

     

    Binding bind = new Binding();

    lv.DataContext = cvs; // lv is the name of the listview control

    lv.SetBinding(

    ListView.ItemsSourceProperty, bind);

    I add a groupstyle to the listview to enable the grouping

    GroupStyle gs = new GroupStyle();           
    lv.GroupStyle.Add(gs);
    Next I do the column creation. As you might have noticed, I have four fields. I am grouping by the "ContentType" field. I set up all the columns in code as below.

    GridViewColumn col = new GridViewColumn();
    col.Width = 100;
    col.Header = "Program name";
    col.DisplayMemberBinding = new Binding("Name");
    grid.Columns.Add(col);
    
    
    col = new GridViewColumn();
    col.Width = 100;
    col.Header = "Program provider";
    col.DisplayMemberBinding = new Binding("ProvName");
    grid.Columns.Add(col);
    
    //col = new GridViewColumn();
    //col.Width = 75;
    //col.Header = "ContentType";
    //col.DisplayMemberBinding = new Binding("ContentType");
    //grid.Columns.Add(col);
    
    col = new GridViewColumn();
    col.Width = 100;
    col.Header = "Status";
    col.DisplayMemberBinding = new Binding("Status");
    grid.Columns.Add(col); // grid is the name of the gridview
    

    Please note the commented set of columndefinition, bound the the "contenttype" field. this is the field I am grouping by, and wish to have an expander which would expand the collapse the group.

    Kindly comment on how should I proceed.

    Thanks!


    Thursday, November 26, 2009 3:50 PM
  • Do not use

    col.DisplayMemberBinding = new Binding("ProvName");
    

    Here is your required code.


    col = new GridViewColumn();
    col.Width = 75;
    col.Header = "ContentType";
    
    DataTemplate dt = new DataTemplate();
    FrameworkElementFactory expander = new FrameworkElementFactory(typeof(Expander));
    
    Binding binding = new Binding();
    binding.Path = new PropertyPath("ContentType");
    
    FrameworkElementFactory text = new FrameworkElementFactory(typeof(TextBlock));
    text.SetBinding(TextBlock.TextProperty, binding);
    text.SetValue(ForegroundProperty, Brushes.Green);
    text.SetValue(FontSizeProperty, Convert.ToDouble(14));
    expander.AppendChild(text);
    
    dt.VisualTree = expander;
    col.CellTemplate = dt;
    
    grid.Columns.Add(col);
    


    Please let us know if it still doesn't work.

    Zeeshan Amjad
    http://zamjad.wordpress.com/
    • Proposed as answer by Zeeshan Amjad Friday, November 27, 2009 4:28 AM
    • Marked as answer by Jim Zhou - MSFT Tuesday, December 01, 2009 8:27 AM
    Friday, November 27, 2009 4:28 AM
  • Hi Zeeshan, your code code doesn't quite do what I am looking at (or may be, I have put it in the worng place ...)

    I am trying to group the data by the by one of the fields, and want the expander to appear in the group header, and that the group could be collapsed/expanded by the expander.

    As I can't post a screenshot, I am trying my best to describe what I have (before applying your code), what I want  and and what I am getting  after ...

    What i have now:
    (Assume that the parameter in Col3 is the one by which I am grouping)
    Col 1    Col2   Col3   Col4
    ------------------------------
    Group1
    Data 1   ...      ...       ...
    Data 2   ...      ...       ...
    Data 3   ...      ...       ...
    Data 4   ...      ...       ...
    Group2
    Data 1   ...      ...       ...
    Data 2   ...      ...       ...
    Data 1   ...      ...       ...
    Group3
    Data 2   ...      ...       ...
    Data 3   ...      ...       ...
    Data 4   ...      ...       ...

    What I want
    Col 1    Col2   Col3   Col4
    ------------------------------
    <Expander> Group1
    Data 1   ...      ...       ...
    Data 2   ...      ...       ...
    Data 3   ...      ...       ...
    Data 4   ...      ...       ...
    <Expander>Group2
    Data 1   ...      ...       ...
    Data 2   ...      ...       ...
    Data 1   ...      ...       ...
    <Expander>Group3
    Data 2   ...      ...       ...
    Data 3   ...      ...       ...
    Data 4   ...      ...       ...

    What I am getting after I apply your code:

    Col 1    Col2   Col3   Col4
    ------------------------------
    Group1
    Data 1   ...      <Exp> ...
    Data 2   ...     <Exp> ...
    Data 3   ...      <Exp> ...
    Data 4   ...      <Exp> ...
    Group2
    Data 1   ...      <Exp> ...
    Data 2   ...     <Exp> ...
    Data 1   ...      <Exp> ...
    Group3
    Data 2   ...      <Exp> ...
    Data 3   ...      <Exp> ...
    Data 4   ...      <Exp> ...

    Effectively, I am getting an expander button in the data cell of that particular column, wheras I want in the group header.




    Friday, November 27, 2009 7:01 AM
  • Hello, anybody could help out, please!

    Monday, November 30, 2009 12:58 PM
  • Hi Xl3k,

    I recommend you not to do all the logic using pure code if there is not special reason, the following is a ready-to-run example of using the grouging, please check it out.

    Code snippet:

     

    <Window.Resources>

            <!--xml data-->

            <XmlDataProvider x:Key="MyData" XPath="/Info/Item">

                <x:XData>

                    <Info xmlns="">

                        <Item ID="12345" Name="Book 1" Price="$32.05"

                    Author="Author A" Catalog="Business"/>

                        <Item ID="13590" Name="Book 2" Price="$10.00"

                    Author="Author B" Catalog="Language"/>

                        <Item ID="24678" Name="Book 3" Price="$9.00"

                    Author="Author C" Catalog="Language"/>

                        <Item ID="65432" Name="Book 4" Price="$8.50"

                    Author="Author D" Catalog="Business"/>

                        <Item ID="11233" Name="Book 5" Price="$19.00"

                    Author="Author E" Catalog="Health"/>

                        <Item ID="94837" Name="Book 6" Price="$8.50"

                    Author="Author F" Catalog="Language"/>

                    </Info>

                </x:XData>

            </XmlDataProvider>

            <!--CollectionViewSource-->

            <CollectionViewSource x:Key='src'

                              Source="{Binding Source={StaticResource MyData}}">

                <CollectionViewSource.GroupDescriptions>

                    <PropertyGroupDescription PropertyName="@Catalog" />

                </CollectionViewSource.GroupDescriptions>

            </CollectionViewSource>

        </Window.Resources>

        <!--Content-->

        <ListView ItemsSource='{Binding Source={StaticResource src}}'

                BorderThickness="0">

            <ListView.GroupStyle>

                <!--ObservableCollection<GroupStyle>-->

                <GroupStyle>

                    <GroupStyle.ContainerStyle>

                        <Style TargetType="{x:Type GroupItem}">

                            <Setter Property="Margin" Value="0,0,0,5"/>

                            <Setter Property="Template">

                                <Setter.Value>

                                    <ControlTemplate TargetType="{x:Type GroupItem}">

                                        <Expander IsExpanded="True"

                                                  BorderBrush="#FFA4B97F"

                                                  BorderThickness="0,0,0,1">

                                            <Expander.Header>

                                                <DockPanel>

                                                    <TextBlock FontWeight="Bold" Text="{Binding Path=Name}"

                                       Margin="5,0,0,0" Width="100"/>

                                                    <TextBlock FontWeight="Bold"

                                       Text="{Binding Path=ItemCount}"/>

                                                </DockPanel>

                                            </Expander.Header>

                                            <Expander.Content>

                                                <ItemsPresenter />

                                            </Expander.Content>

                                        </Expander>

                                    </ControlTemplate>

                                </Setter.Value>

                            </Setter>

                        </Style>

                    </GroupStyle.ContainerStyle>

                </GroupStyle>

            </ListView.GroupStyle>

            <ListView.View>

                <GridView>

                    <GridViewColumn Header="ID"

                            DisplayMemberBinding="{Binding XPath=@ID}"

                            Width="100" />

                    <GridViewColumn Header="Name"

                            DisplayMemberBinding="{Binding XPath=@Name}"

                            Width="140" />m

                    <GridViewColumn Header="Price"

                            DisplayMemberBinding="{Binding XPath=@Price}"

                            Width="80" />

                    <GridViewColumn Header="Author"

                            DisplayMemberBinding="{Binding XPath=@Author}"

                            Width="80" />

                </GridView>

            </ListView.View>

        </ListView>

    For creating group style in code behind, you can see this link:
    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9889a0b7-dc9c-4f80-b92d-0a2e761e82b6

    Any issues with this, please feel free to feed back.
    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Tuesday, December 01, 2009 8:34 AM
  • First, lots of thanks to Jim for helping out :)

    I want to achieve the same thing in code. I have done everything except the except the expander buttons and wonder how difficult could that be. I believe there would be many out there would would like to do it entirely in code ...


    My requirements are like
    1. My datasource would be changing ('schema' would reamin the same anyway)
    2. The field by which the grouping happens would be changing
    3. Last but not least, the columns would be added/removed, but I guess this isn't too much of a problem here ...

    Thats why I feel it would be a good idea if everything could be done in code. Do you think its better/easier to achieve all this in xaml?

    Thanks again!

    Wednesday, December 02, 2009 1:20 PM
  • Hi xl3k,
    -->Thats why I feel it would be a good idea if everything could be done in code. Do you think its better/easier to achieve all this in xaml?

    When developing WPF application, we can take advantage of the style, templating and data binding functinalities, etc WPF provides you may already noticed that doing this in XAML markup is more readable and maintainable, while it's not readable in code behind, however, I am saying that we need to do everthing in XAML markup, sometimes, code-behind is needed, it depends on your particular scenario.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Tuesday, December 08, 2009 8:27 AM
  • Hi, Could you please explain what this part of the xaml does:

    <Expander.Content>
           <ItemsPresenter />
    </Expander.Content>
    
    

    ... and how would it be possible to implement in in code?

    I tried something as below but it doesn't work ...
    FrameworkElementFactory expander = new FrameworkElementFactory(typeof(Expander));
    ...           
    expander.SetValue(Expander.ContentProperty, typeof(ItemsPresenter));

    Friday, December 18, 2009 1:52 PM
  • Hi Xl3k,

    --><Expander.Content>
           <ItemsPresenter />
    </Expander.Content>

    The code above means that you are defining a items container presenter, usually the visual tree looks like below:
    ItemsPresenter
       ItemsPanel(StackPanel,WrapPanel...)
           Items container(Like ListBoxItem,ComboBoxItem..)
              .....

    -->I tried something as below but it doesn't work ...
    I posted an analogous thread in the below link, including XAML markup and the corresponding code-behind, please check it out:
    http://social.msdn.microsoft.com/Forums/en-SG/wpf/thread/9889a0b7-dc9c-4f80-b92d-0a2e761e82b6

    Any issues, please feel free to ask.
    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Monday, December 21, 2009 9:21 AM
  • Hi Jim,

    I have seen the article you mentioned above. I wonder  how it would work as I don't see any code to connect the expander factory to the control template somewhere? Don't we need something like -

    template.VisualTree = factoryExpander; (actually this too doesn't help, but there has to be something like this I believe ...)


    (I added similar comment in that thread too)


    Thanks.
    Thursday, January 07, 2010 8:20 AM