1

I had this in my xaml that formatted my listView. That works but I have to add a mode complicated logic so that this has to be applied

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel Orientation = "Horizontal" Width = "250" Background = "{x:Null}"  VerticalAlignment = "Top"></WrapPanel>
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

so now I'd need to apply the code above in code behind.

---EDIT for Martino Bordin---

Please tell me what I have misunderstood:

1a. I have defined a style in my listview:

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
    <ListView.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate >
                        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

2a.I set it in my code behind only when I need it:

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
    <ListView.Resources>
        <Style x:Key="ListViewStyle" TargetType="ListView">
            <Setter Property="ItemsPanel">
                <Setter.Value>
                    <ItemsPanelTemplate >
                        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"/>
                    </ItemsPanelTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ListView.Resources>

    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>


lvPPtab1.Style = (Style)this.Resources["ListViewStyle"];

and all what I see is... nothing listView empty.

Then I tried to stick to what you said and so I did that:

1b. in the xaml

<ListView x:Name="lvPPtab1" Grid.Row="2" FontSize="12" Background="{x:Null}"  BorderBrush="Gainsboro" BorderThickness="5" Margin="10,12.2,10,8.4" VerticalAlignment="Stretch" PreviewMouseLeftButtonDown="ListBox_PreviewMouseLeftButtonDown" SelectionChanged="ListView_SelectionChanged">
<ListView.Resources>
    <ItemsPanelTemplate x:Key="ListViewStyle" >
        <WrapPanel Orientation="Horizontal" VerticalAlignment="Top"></WrapPanel>
    </ItemsPanelTemplate>
</ListView.Resources>
<ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="Blue"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>
  1. in the code behind:

    lvPPtab1.ItemsPanel = (ItemsPanelTemplate)this.Resources["ListViewStyle"];

but again nothing! All empty where am I wrong?????

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • What code?what logic?.can you please tell us what are you trying to acheive? – Rohit Apr 13 '16 at 07:38
  • You can find everything here in my previous question: http://stackoverflow.com/questions/36579473/continuation-to-how-can-i-programmatically-create-a-listview-full-of-strings-co?noredirect=1#comment60762700_36579473 – Patrick Apr 13 '16 at 07:42
  • I tried it in the past without luck. – adminSoftDK Apr 13 '16 at 07:59

1 Answers1

3

Put the template in the resources dictionary, give a x:Name to your listview, then you can access its properties in the code behind:

myListView.ItemsPanel
myListView.ItemsPanel = (ItemsPanelTemplate)this.Resources["MyListViewPanelTemplate"];
Martino Bordin
  • 1,412
  • 1
  • 14
  • 29
  • Thank you for your answer but that is not what I am asking. I might not have been clear. Ok myListView.Items panel and then the rest??? – Patrick Apr 13 '16 at 07:39
  • I have not been the one(s) who downvoted you. That being said you solution looks interesting. But how to move the code I put above from the peculiar listview part to resources? In other words how to make it general? – Patrick Apr 13 '16 at 13:18
  • Check this link http://stackoverflow.com/questions/5705666/listview-define-itemspaneltemplate-in-resource-dictionary – Martino Bordin Apr 13 '16 at 13:20
  • I don't see the code where you are populating the listview – Martino Bordin Apr 14 '16 at 09:09
  • The code is not important in any case I have two situations: (a). "explorer with icons like" and in this case I populate it with a border with textbox and images. (b). "grid like" only textbox. You can populate it with "AAAA" "BBBB". I've gone further and put it in the window resources and it works!!!!!! Thanks a million!!!! We nearly are there. There only are two additional problems: 1. In case (b) I have to remove the items panel and. I tried to set it to null but it didn't work. 2. In case (a) I can't get to set items width. thanx – Patrick Apr 14 '16 at 09:49
  • OK Problem 2. solved!!!!!!! Just got to know how to programmatically remove itemspanel – Patrick Apr 14 '16 at 10:00