Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a listview which -only under certain circumnstances- has to list items horizontally. The content is built programmatically.

I don't think that this changes but the xaml is:

XML
<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.Effect>
		<DropShadowEffect ShadowDepth="4" Direction="330" Color="Black" Opacity="0.5" BlurRadius="4"/>
	</ListView.Effect>
										<ListView.ItemContainerStyle>
		<Style TargetType="ListViewItem">
	<Style.Triggers>
		<Trigger Property="IsSelected" Value="True">
			<Setter Property="Background" Value="Blue"/>
									</Trigger>
											</Style.Triggers>
			</Style>
		</ListView.ItemContainerStyle>
	</ListView>


What I have tried:

In another case to do the same (but always) I do

XML
<ListView.ItemsPanel >
	<ItemsPanelTemplate >
		<WrapPanel Orientation="Horizontal" Width="250"  VerticalAlignment="Top"></WrapPanel>
	</ItemsPanelTemplate>
</ListView.ItemsPanel>


and that works always. Now I have to do it only under certain circumstances so I have to do it sometimes and have to undo under others.
Posted
Updated 13-Apr-16 7:18am

You should be able to use WPF data binding to the Orientation property of the Wrap Panel.
You just need a property in your DataContext that provides the desired orientation.
XML
<WrapPanel Orientation="{Binding Path=ListViewOrientation, FallbackValue=Horizontal}"
           VerticalAlignment="Top" />

Here I called it ListViewOrientation. It really ought to be named something relative to your problem/solution space. That it also affects the display of the ListView is an aside. ;-)

If you're going to be changing multiple properties of the template, you should look into a DataTemplateSelector to see if that does what you want.
 
Share this answer
 
v2
Comments
Patrick70__ 13-Apr-16 17:33pm    
I might go wrong but is that code behind? All the trick is changing it in code behind instead than in xaml
If I'm not much mistaken, you know how to achieve certain layout statically in XAML, but not sure how to implement the same effect in code behind and also switch it depending on conditions.

Without going to the detail, I can suggest you very general method of finding it out. You need to understand what happens to XAML if this sort (Window, Page, UserControl) — the XAML code is translated to C# (or whatever the application implementation language is) and then compiled as any other code.

You will find this auto-generated code under the project's sub-directory; just take a look or use file search. It will be some *.cs file which are not part of your source code.

Besides, this is a useful exercise, to see how code originated from XAML works.

—SA
 
Share this answer
 
Comments
Patrick70__ 13-Apr-16 11:48am    
If I could elaborate this, it would be the best hint ever since I could reuse it countless times. To find the file you are talking about since we are talking about ListViewItem, I have searched all files containing "ListViewItem" but only MainView.xaml appeared. So colour my dumb but something escapes me now. Would you please help me? thanx a lot
Sergey Alexandrovich Kryukov 13-Apr-16 11:57am    
Help? I don't know. I did not understand the detail. Perhaps you need to describe the scenario in detail and re-write "What I have tried". As your idea is to move code behind, you have to try to write code behind there. Create a separate prototype project, as simple as possible, focusing on one single issue. Such projects can be easily fit in exactly two files: one C# and one XAML, or, without XAML, in just one. Simplifying it down to one single issue is the key?
—SA
Sergey Alexandrovich Kryukov 13-Apr-16 12:07pm    
Oh, I just realized that you, by some reason, could not find the file. It surprises me. First of all, do you have any decent file manager, at least a bit better than Exporer (I really fail to understand how people can tolerate standard Windows harness like that)? Even if not, file search is still trivial.

I'll have to see by building one of my projects...

Let's see... Start with the directory where your .csproj is. This sub-directory contains other ones with your source code. After build, it adds another one, 'obj'. Get inside it... We can have obj/Debug, obj/Release, or both, or more. Look at the one with the configuration name you most recently built. In this code sample, I have only one XAML with main window named "MainWindow". Let's say I built "Debug", so I look at obj/Debug and find the sub-directory obj/Debug/Ui, with two files: obj/Debug/Ui/MainWindow.baml and obj/Debug/Ui/MainWindow.g.cs.

Your names may vary, but the idea is simple. (Didn't you simply forget to build the solution?) You should be able to find your files by yourself and see which file is for what, from context...

—SA
Patrick70__ 13-Apr-16 14:36pm    
Again thank you for taking the time to help me. If you were right I'd be the happiest man on earth (and not only me) for I would have a safe and sure way to convert from xaml to cs. Your idea may be partially right. I am using a program that allows me to find files wherever under the dir of my project. But I searched for files containing a keyword. At the moment the listview which has in xaml the ability to go horizontal is0 named lvAllowedPPtab2 so I searched for this in whatever files under obj.As you said in Mainview.g.cs i found the event linked with that listview so: case 51:
this.lvAllowedPPtab2 = ((System.Windows.Controls.ListView)(target));

#line 364 "..\..\..\..\Views\MainView\MainView.xaml"
this.lvAllowedPPtab2.PreviewMouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(this.ListBox_PreviewMouseLeftButtonDown);

#line default
#line hidden

#line 364 "..\..\..\..\Views\MainView\MainView.xaml"
this.lvAllowedPPtab2.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(this.ListView_SelectionChanged);

but nothing related to ItemsPanelTemplate. So i searched for ItemsPanelTemplate in whatever file under obj and ... alas...only in mainview.xaml.
I feel that your idea is right and that might make my day ... no my month... but something escapes me (hope only me not you also...)
Sergey Alexandrovich Kryukov 13-Apr-16 15:47pm    
What do you mean? It's not "converting"; it's showing the code which is actually compiling; I successfully used it.
All you need is to look at the code with attention. Forget your keywords; see how it really works.
After all, there is no such thing as miracle — all your code is there.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900