1

What I have here is a ListView that shows as an item another ListView with some more items. So ill try to explain better. For every lines of the ListViewMother I want a Listview linked to the datasource of lines. So what i want to try later is to link the Page to a particular template given by a string called templatestyle that will be given to the ListviewMother to select the different ListStyle. So my question is how to I link a datasource to a templated Listview?

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
   Title = "MainWindow" Height = "350" Width = "604">

    <Grid>
        <ListView Name="ListviewMother">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="20">
                        <TextBlock Text="aaaaaaa"></TextBlock>
                        <ListView ItemsSource="{Binding lines}" Background="red"> (listitemson)
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <Grid Margin="20" >
                                        <TextBox Text="aaaaaaaaaa" Margin="20"/>
                                        <TextBox Text="{Binding linenumber}" />
                                    </Grid>

                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                    </Grid>
                </DataTemplate>
            
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

</Window> 

The c# code

namespace WpfApp2
{

    public partial class MainWindow : Window
    {
        public ObservableCollection<Page> pages = new ObservableCollection<Page>();
        public ObservableCollection<line> lines { get; set; } = new ObservableCollection<line>();
        public MainWindow()

        {
            lines.Add(new line() { linenumber = "tsadssdest" });
            lines.Add(new line() { linenumber = "tedsfdsfdsst" });
            pages.Add(new Page() { Title = "asdsad"});
            pages.Add(new Page() { Title = "sdsadad"});
            pages.Add(new Page() { Title = "gggdfgs"});
            InitializeComponent();
            ListviewMother.ItemsSource = pages;



        }
    }
}
public class Page
{
    public string Title { get; set; }
    public string TemplateStyle { get; set; }


}

public class line
{
    public string linenumber { get; set; }

}
5
  • 2
    Are you going to show the same lines collection in each Page? Otherwise, ObservableCollection<line> lines should be a member of the Page class. Commented Sep 15, 2020 at 8:57
  • this works well but you must have a reference in Page to line.... I was thinking about a way to have this flexible. Because every template will every different binding names (line have linenumer... if I want to connect something else I really dont know how) Commented Sep 15, 2020 at 9:20
  • What does that mean? Do you want a single line collection, or one per Page? It can only be one or the other. Commented Sep 15, 2020 at 9:22
  • One per page. This is what blocks me.... How do you achieve that? How do you achieve a single line collection per page that is custom for every page? for example first page you have line the second one will be letters.... and all of this needs a reference into page! I try to do something dynamic.... Like every page already reference to a class without having a reference in Page. Commented Sep 15, 2020 at 9:59
  • 1
    Then the lines property should simply be a Page member. Commented Sep 15, 2020 at 10:01

1 Answer 1

1

You need to put the collection of lines in your Page class.I do some modification for your code to show the ListView inside in ListView.

The xaml code is:

<Grid>
    <ListView Name="ListviewMother">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Margin="20">
                    <TextBlock Text="{Binding Title}"></TextBlock>
                    <ListView ItemsSource="{Binding lines}" Background="red">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid Margin="2" >
                                    <TextBox Text="{Binding linenumber}" />
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

The cs code is:

public partial class MainWindow : Window
{
    public ObservableCollection<Page> pages = new ObservableCollection<Page>();
    public MainWindow()
    {
        pages.Add(new Page() { Title = "Page1",lines=new ObservableCollection<line>() { new line() { linenumber="Line1_1"}, new line() { linenumber = "Line1_2" }, new line() { linenumber = "Line1_3" } } });
        pages.Add(new Page() { Title = "Page2",lines=new ObservableCollection<line>() { new line() { linenumber="Line2_1"}, new line() { linenumber = "Line2_2" }, new line() { linenumber = "Line2_3" } } });
        pages.Add(new Page() { Title = "Page3" });
        InitializeComponent();
        ListviewMother.ItemsSource = pages;
    }
}
public class Page
{
    public string Title { get; set; }
    public string TemplateStyle { get; set; }
    public ObservableCollection<line> lines { get; set; } = new ObservableCollection<line>();

}

public class line
{
    public string linenumber { get; set; }
}

The result picture is like this: enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.