要查看英语原文,请勾选“英语”复选框。也可将鼠标指针移到文本上,在弹出窗口中显示英语原文。
翻译
英语
本文档已存档,并且将不进行维护。

FrameworkElement.MaxWidth 属性

获取或设置元素的最大宽度约束。

命名空间:  System.Windows
程序集:  PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
[TypeConverterAttribute(typeof(LengthConverter))]
public double MaxWidth { get; set; }
<object MaxWidth="double"/>
- or -
<object MaxWidth="qualifiedDouble"/>

XAML 值

double

Double

Double 值的字符串表示形式大于等于 0.0。 它被解释为device-independent unit (1/96th inch)度量值。 字符串无需显式包含小数点。 例如,值 1 是可接受的。

在“属性值”一节中提到的相同 Double 范围限制也适用;不同之处在于,必须使用 x:Static 标记扩展 将该值设置为 PositiveInfinity

qualifiedDouble

如上所述的一个 double 值,后跟下列单位声明字符串之一:pxincmpt

px(默认值)表示device-independent units (1/96th inch per unit)。

in 表示英寸;1in==96px

cm 表示厘米;1cm==(96/2.54) px

pt 表示磅;1pt==(96/72) px

属性值

类型:System.Double
元素的最大宽度,单位是device-independent units (1/96th inch per unit)。 默认值为 PositiveInfinity 此值可以是任何大于等于 0.0 的值。 PositiveInfinity 也有效。

标识符字段

MaxWidthProperty

设置为 true 的元数据属性

AffectsMeasure

这是 FrameworkElement 上的三个用于指定宽度信息的属性之一。 另外两个是 MinWidthWidth 如果这三个值之间存在冲突,则应用程序确定宽度的实际顺序是:首先必须采用 MinWidth;然后采用 MaxWidth;最后,如果这些值中的每个值都在限制之内,则采用 Width

Double 值上的限制是由 ValidateValueCallback 机制强制实施的。 如果尝试设置一个无效值,则会引发运行时异常。

This example visually shows the differences in rendering behavior among the four width-related properties in Windows Presentation Foundation (WPF).

The FrameworkElement class exposes four properties that describe the width characteristics of an element. These four properties can conflict, and when they do, the value that takes precedence is determined as follows: the MinWidth value takes precedence over the MaxWidth value, which in turn takes precedence over the Width value. A fourth property, ActualWidth, is read-only, and reports the actual width as determined by interactions with the layout process.

The following Extensible Application Markup Language (XAML) examples draw a Rectangle element (rect1) as a child of Canvas. You can change the width properties of a Rectangle by using a series of ListBox elements that represent the property values of MinWidth, MaxWidth, and Width. In this manner, the precedence of each property is visually displayed.


<Canvas Height="200" MinWidth="200" Background="#b0c4de" VerticalAlignment="Top"  HorizontalAlignment="Center" Name="myCanvas">
    <Rectangle HorizontalAlignment="Center" Canvas.Top="50" Canvas.Left="50"  Name="rect1" Fill="#4682b4" Width="100" Height="100"/>
</Canvas>


...


    <TextBlock Grid.Row="1" Grid.Column="0" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle Width:</TextBlock>
    <ListBox Grid.Column="1" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeWidth">
      <ListBoxItem>25</ListBoxItem>
      <ListBoxItem>50</ListBoxItem>
      <ListBoxItem>75</ListBoxItem>
      <ListBoxItem>100</ListBoxItem>
      <ListBoxItem>125</ListBoxItem>
      <ListBoxItem>150</ListBoxItem>
      <ListBoxItem>175</ListBoxItem>
      <ListBoxItem>200</ListBoxItem>
      <ListBoxItem>225</ListBoxItem>
      <ListBoxItem>250</ListBoxItem>
    </ListBox>

	<TextBlock Grid.Row="1" Grid.Column="2" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MinWidth:</TextBlock>
    <ListBox Grid.Column="3" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeMinWidth">
      <ListBoxItem>25</ListBoxItem>
      <ListBoxItem>50</ListBoxItem>
      <ListBoxItem>75</ListBoxItem>
      <ListBoxItem>100</ListBoxItem>
      <ListBoxItem>125</ListBoxItem>
      <ListBoxItem>150</ListBoxItem>
      <ListBoxItem>175</ListBoxItem>
      <ListBoxItem>200</ListBoxItem>
      <ListBoxItem>225</ListBoxItem>
      <ListBoxItem>250</ListBoxItem>
  </ListBox>      

    <TextBlock Grid.Row="1" Grid.Column="4" Margin="10,0,0,0" TextWrapping="Wrap">Set the Rectangle MaxWidth:</TextBlock>
    <ListBox Grid.Column="5" Grid.Row="1" Margin="10,0,0,0" Width="50" Height="50" SelectionChanged="changeMaxWidth">
      <ListBoxItem>25</ListBoxItem>
      <ListBoxItem>50</ListBoxItem>
      <ListBoxItem>75</ListBoxItem>
      <ListBoxItem>100</ListBoxItem>
      <ListBoxItem>125</ListBoxItem>
      <ListBoxItem>150</ListBoxItem>
      <ListBoxItem>175</ListBoxItem>
      <ListBoxItem>200</ListBoxItem>
      <ListBoxItem>225</ListBoxItem>
      <ListBoxItem>250</ListBoxItem>  
    </ListBox>


The following code-behind examples handle the events that the SelectionChanged event raises. Each custom method takes the input from the ListBox, parses the value as a Double, and applies the value to the specified width-related property. The width values are also converted to a string and written to various TextBlock elements (definition of those elements is not shown in the selected XAML).


private void changeWidth(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
    Double sz1 = Double.Parse(li.Content.ToString());
    rect1.Width = sz1;
    rect1.UpdateLayout();
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
    txt2.Text = "Width is set to " + rect1.Width;
    txt3.Text = "MinWidth is set to " + rect1.MinWidth;
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}
private void changeMinWidth(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
    Double sz1 = Double.Parse(li.Content.ToString());
    rect1.MinWidth = sz1;
    rect1.UpdateLayout();
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
    txt2.Text = "Width is set to " + rect1.Width;
    txt3.Text = "MinWidth is set to " + rect1.MinWidth;
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}
private void changeMaxWidth(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
    Double sz1 = Double.Parse(li.Content.ToString());
    rect1.MaxWidth = sz1;
    rect1.UpdateLayout();
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth;
    txt2.Text = "Width is set to " + rect1.Width;
    txt3.Text = "MinWidth is set to " + rect1.MinWidth;
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth;
}


For the complete sample, see Width Properties Comparison Sample.

.NET Framework

受以下版本支持:4、3.5、3.0

.NET Framework Client Profile

受以下版本支持:4、3.5 SP1

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2

.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求
显示: