C#WPF如何在后面的代码中将listview设置为水平 [英] C# WPF how to set a set a listview to horizontal in code behind

查看:278
本文介绍了C#WPF如何在后面的代码中将listview设置为水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图 - 只在某些情况下 - 必须水平列出项目。内容以编程方式构建。



我不认为这会改变,但xaml是:



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:

<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

<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.

推荐答案

你应该能够使用WPF数据绑定到Wrap Panel的Orientation属性。

你只需要一个提供所需方向的 DataContext 中的属性。

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.
<WrapPanel Orientation="{Binding Path=ListViewOrientation, FallbackValue=Horizontal}"
           VerticalAlignment="Top" />



我在这里称它为 ListViewOrientation 。它应该被命名为相对于你的问题/解决方案空间的东西。它也会影响ListView的显示。 ;-)



如果您要更改模板的多个属性,您应该查看DataTemplateSelector以查看它是否符合您的要求。


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.


如果我没弄错,你知道如何在XAML中静态地实现某些布局,但不知道如何在后面的代码中实现相同的效果,并根据条件切换它。 />


如果不详细说明,我可以建议你找到它的一般方法。如果这种排序(Window,Page,UserControl) - XAML代码被转换为C#(或任何应用程序实现语言),然后编译为任何其他代码,您需要了解XAML会发生什么。



您将在项目的子目录下找到此自动生成的代码;只是看看或使用文件搜索。这将是一些* .cs文件,它不是源代码的一部分。



此外,这是一个有用的练习,看看代码源自XAML的工作原理。



-SA
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


这篇关于C#WPF如何在后面的代码中将listview设置为水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆