I needed to be able to quickly and easily change the display of items in the ListBox . Of course, everything in it is very inconvenient, if the display often has to be changed, add new templates, and so on. Then I began to look towards the ListView with its View property.

However, there is very little information on the Internet about its use and it is very scarce. On MSDN only superficial description.

Here it is more, but much is again not clear. They redefined the ViewBase class by adding the public DataTemplate ItemTemplate property to it. But I can not understand how this property is used, because it does not exist in the ViewBase , which means it needs to be sent to the ListView.ItemTemplate . But how it is done there is not said.

In addition, you also need to change the panel on the run ( ItemsPanelTemplate ). Somewhere you need a WrapPanel , somewhere a StackPanel , somewhere in general a UniformGrid .

Following the logic, I added the ItemsPanelTemplate PanelTemplate property from the ViewBase to my class. But what to do with this next? In short, how can I ensure that I change the View property of a ListView , pointing to a resource, and the ListView display (the panel and all elements) changes completely?

  • there you can browse pages on the site below, can this continuation? - user2455111
  • It seems necessary to create a resource dictionary with the name generic.xaml like this :-) - ogorank
  • Itself just started tormented by mobile phones. I would venture to offer. Make a custom adapter and there in the function context.LayoutInflater.Inflate (VIEW, null); You can supply your view id - Dimka
  • @ user2455111, there is a poor description .. I can’t catch up with what is used to do by analogy. - iRumba
  • @ogorank, yeah, but I have another little task and I need to figure out what is written there to do by analogy. "The TileView style applied to the ListView makes three changes listed below: .......... Changes the ItemsPanel from StackPanel to WrapPanel, allowing for tiling. The WrapPanel is set to the width of the ListView." WHERE IS IT??? Point your finger at a piece of code or markup? - iRumba

1 answer 1

If I understand the question correctly, you need this:

 <Window x:Class="MyApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="150" Width="150"> <Window.Resources> <!-- опрСдСляСм Π½ΡƒΠΆΠ½Ρ‹Π΅ ΡˆΠ°Π±Π»ΠΎΠ½Ρ‹ --> <DataTemplate x:Key="Cyan"> <TextBlock Text="{Binding}" FontWeight="Bold" Foreground="DarkCyan"/> </DataTemplate> <DataTemplate x:Key="Red"> <TextBlock Text="{Binding}" FontStyle="Italic" Foreground="Red"/> </DataTemplate> </Window.Resources> <Grid> <!-- примСняСм ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π½ΠΈΡ… --> <ListView ItemsSource="{Binding}" Name="LV" ItemTemplate="{StaticResource Red}"/> <!-- ΠΊΠ½ΠΎΠΏΠΊΠ° для ΠΏΠ΅Ρ€Π΅ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ --> <Button Click="OnStyleChangeReq" VerticalAlignment="Bottom" Content="Change style"/> </Grid> </Window> 

and code-behind:

 bool red = true; void OnStyleChangeReq(object sender, RoutedEventArgs e) { var template = (DataTemplate)FindResource(red ? "Cyan" : "Red"); red = !red; LV.ItemTemplate = template; } 

It turns out like this:

animated cartoon

Well, or you can switch through triggers and styles, if you need to switch depending on the attached data from the VM.

  • Not that. With this approach, ListView not needed, the ListBox would be enough. I need to work with its View property. The point is to be able to easily change the preset templates not only items, but also the container. I wrote about this in the question. "In addition, you also need to change the panel on the run (ItemsPanelTemplate). Somewhere you need a WrapPanel, somewhere a StackPanel, somewhere in general a UniformGrid." In this case, 2 properties will have to be changed. ItemTemplate and ItemsPanel. - iRumba
  • @iRumba: Well, yes. Then why not put the containers in style and replace it? - VladD
  • I think because I don't need to update all the styles ... they can be pretty heavy. In addition, they carry a whole bunch of properties that have not been changed. - iRumba