简体   繁体   English

如何在WPF中创建自适应布局?

[英]How can I create an adaptive layout in WPF?

A website can be designed to adapt to smaller screen sizes using media queries. 可以设计网站以使用媒体查询来适应较小的屏幕尺寸。 For example, three columns on a wide screen, one column on a low-resolution phone. 例如,宽屏幕上的三列,低分辨率手机上的一列。

Is there a similar technique for WPF to adjust the layout based on available screen space or parent control size? 是否有类似的技术让WPF根据可用的屏幕空间或父控件大小调整布局?

For example, I'd like to have 3 columns displayed horizontally on a large screen, but displayed vertically on smaller screen. 例如,我希望在大屏幕上水平显示3列,但在较小的屏幕上垂直显示。 Ideally, I'd like to formulate layouts like this: "If this control's width is less than 400 points, rearrange these controls in that way." 理想情况下,我想制定如下布局:“如果此控件的宽度小于400点,则以这种方式重新排列这些控件。”

How can I create an adaptive design like this in WPF? 如何在WPF中创建这样的自适应设计? That is, define different layouts for controls for specific parent control sizes? 也就是说,为特定父控件大小的控件定义不同的布局?

Ideally controls should be rearranged instead of duplicated or recreated, to avoid being extremely slow. 理想情况下,控件应该重新排列而不是重复或重新创建,以避免极其缓慢。

The easiest way to do this is with DataTriggers and a Converter to test if the bound value is greater or less than a parameter. 最简单的方法是使用DataTriggersConverter来测试绑定值是大于还是小于参数。

This will allow you to easily adjust the style setters based on a bound value. 这将允许您根据绑定值轻松调整样式设置器。 For example, you could use 例如,你可以使用

<Style x:Key="MyControlStyle">
    <!-- Default Values -->
    <Setter Property="Grid.Row" Value="0" />
    <Setter Property="Grid.Column" Value="0" />
    <Style.Triggers>
        <DataTrigger Value="True" 
                     Binding="{Binding ActualHeight, ElementName=MyWindow, 
                         Converter={StaticResource IsValueLessThanParameter}, 
                         ConverterParameter=400}">
            <!-- Values to use when Trigger condition is met -->
            <Setter Property="Grid.Row" Value="1" />
            <Setter Property="Grid.Column" Value="1" />
        </DataTrigger>
    </Style.Triggers>
</Style>

In the case where you have a more complex layout with many pieces that change based on some triggered value, you can replace entire Templates with your trigger instead of just individual properties 如果您有一个更复杂的布局,其中许多部分会根据某些触发值进行更改,则可以使用触发器而不是单个属性替换整个模板

<Style x:Key="MyContentControlStyle" TargetType="{x:Type ContentControl}">
    <Setter Property="ContentTemplate" Value="{StaticResource BigTemplate}" />
    <Style.Triggers>
        <DataTrigger Value="True" 
                     Binding="{Binding ActualHeight, ElementName=MyWindow, 
                         Converter={StaticResource IsValueLessThanParameter}, 
                         ConverterParameter=400}">
            <Setter Property="ContentTemplate" Value="{StaticResource LittleTemplate}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

I believe you can also bind to the SystemParameters object to use additional information about the application in your bindings, although I can't remember the exact syntax for it right now. 我相信你也可以绑定到SystemParameters对象,以便在绑定中使用有关应用程序的其他信息,尽管我现在还记不起它的确切语法。

如果你正在使用WPF的UWP风格,那么你可以使用AdaptiveTrigger

<AdaptiveTrigger MinWindowWidth="720" MinWindowHeight="900" />

The only way I know to do something like this is in code, and you'll need to create a custom layout. 我知道做这样的事情的唯一方法是在代码中,你需要创建一个自定义布局。 The simplest way of doing that is to create a new class that inherits from Panel, and implement MeasureOverride and ArrangeOverride. 最简单的方法是创建一个继承自Panel的新类,并实现MeasureOverride和ArrangeOverride。 I've done custom layouts before, and they can end up being a rather large pain to get working for all cases. 我之前已经完成了自定义布局,并且它们最终会成为一个相当大的痛苦,以适应所有情况。 If you Google "wpf custom layout" you'll get some good examples to start with. 如果你谷歌“wpf自定义布局”,你会得到一些很好的例子。 Given all the functionality you want, you'll definitely have your work cut out for you. 鉴于您想要的所有功能,您肯定会为您完成工作。 You'll probably want to look at attached properties to see about putting annotations in the xaml to give your code an idea of what should be included at the different sizes. 您可能希望查看附加属性,以了解如何在xaml中添加注释,以便让您的代码了解应该包含在不同大小的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM