共用方式為


FrameworkElement.ActualWidth 屬性

定義

取得呈現此項目的寬度。

public:
 property double ActualWidth { double get(); };
public double ActualWidth { get; }
member this.ActualWidth : double
Public ReadOnly Property ActualWidth As Double

屬性值

元素的寬度,以裝置獨立單位為單位的值,每單位 (1/96 英吋) 1/96 英吋。 預設值是 0 (零)。

範例

下列範例會顯示各種寬度屬性。

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;
}
Private Sub changeWidth(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)
    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim sz1 As Double = Double.Parse(li.Content.ToString())
    rect1.Width = sz1
    rect1.UpdateLayout()
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
    txt2.Text = "Width is set to " + rect1.Width.ToString
    txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub
Private Sub changeMinWidth(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim sz1 As Double = Double.Parse(li.Content.ToString())
    rect1.MinWidth = sz1
    rect1.UpdateLayout()
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
    txt2.Text = "Width is set to " + rect1.Width.ToString
    txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub
Private Sub changeMaxWidth(ByVal sender As Object, ByVal args As SelectionChangedEventArgs)

    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim sz1 As Double = Double.Parse(li.Content.ToString())
    rect1.MaxWidth = sz1
    rect1.UpdateLayout()
    txt1.Text = "ActualWidth is set to " + rect1.ActualWidth.ToString
    txt2.Text = "Width is set to " + rect1.Width.ToString
    txt3.Text = "MinWidth is set to " + rect1.MinWidth.ToString
    txt4.Text = "MaxWidth is set to " + rect1.MaxWidth.ToString
End Sub

備註

此屬性是根據其他寬度輸入和版面配置系統的計算值。 此值是由版面配置系統本身根據實際轉譯階段所設定,因此可能會稍微落後一些屬性的設定值,例如 Width 輸入變更的基礎。

因為 ActualWidth 是計算值,所以您應該注意,因為配置系統的各種作業,可能會有多個或累加回報的變更。 配置系統可能會計算子項目所需的測量空間、父項目的條件約束,依此類推。

雖然您無法從 XAML 設定這個屬性,但您可以根據樣式中的值來設定 Trigger

相依性屬性資訊

識別碼欄位 ActualWidthProperty
中繼資料屬性設定為 true

適用於