本文章是由人工翻譯。 將指標移到文章內的文字上方即可查看原文。
譯文
原文
資訊
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

FrameworkElement.Width 屬性

取得或設定項目的寬度。

命名空間:  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 Width { get; set; }
<object Width="double"/>
- or –
<object Width ="qualifiedDouble"/>
- or -
<object Width ="Auto"/>

XAML 值

double

Double

Double 值的字串表示等於或大於 0.0。 如需上限資訊,請參閱「備註」一節。 這個值會解譯為device-independent unit (1/96th inch) 度量。 字串不需明確的包含小數點。 例如,1 是可接受的值。

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

Auto

啟用自動調整大小行為。 請參閱「備註」。

屬性值

型別:System.Double
項目的寬度,以device-independent units (1/96th inch per unit) 為單位。 預設值是 Double.NaN 這個值必須等於或大於 0.0。 如需上限資訊,請參閱「備註」一節。

識別項欄位

WidthProperty

中繼資料屬性設為 true

AffectsMeasure

這是 FrameworkElement 的三個屬性中的其中之一,這三個屬性會指定寬度資訊。 其他兩種屬性為 MinWidthMaxWidth 如果這些值彼此衝突,實際寬度判斷應用的順序如下:您必須先接受 MinWidth,再接受 MaxWidth,最後才是 Width (如果每一個都在界限內)。

這個屬性的傳回值一定會與任何設定給它的值相同。 相反地,ActualWidth 的值可能會不同。 配置可能會由為某些原因而拒絕建議的大小。 同時,相對於屬性系統的 Width 設定,配置系統本身會以非同步方式運作,並且不會處理特定尚未調整大小的變更。

除了可接受 Double 值之外,這個屬性也可以是 Double.NaN 這就是指定自動調整大小行為的方式。 在 XAML 中,您會將這個值設定為字串 "Auto" (不區分大小寫),以啟用自動調整大小行為。 自動調整大小行為隱含著項目將會填入可供其使用的寬度。 請注意,不管特定控制項通常會在其預設樣式中提供的預設值為何,除非特別重新啟用,否則都會停用自動調整大小行為。

除了驗證檢查之外,其中具有非決定性的 Width 上限值,這個值是由配置系統強制設定的 (這是一個非常大的數字,大於 Single.MaxValue,但小於 Double.MaxValue)。 如果您超出此限制,將不會呈現項目,也不會擲回例外狀況。 請勿將 Width 設定為明顯大於任何可用視覺化顯示的最大大小值,否則您將會超出這個非決定性的上限。

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 系統需求

社群新增項目

顯示:
© 2015 Microsoft