如何:设置元素的宽度属性 - WPF


示例:

此示例直观地显示了 Windows Presentation Foundation (WPF) 中四个与宽度相关的属性之间的呈现行为差异。

FrameworkElement 类公开四个描述元素宽度特征的属性。 这四个属性可能会发生冲突,当它们发生冲突时,优先值将按如下方式确定:MinWidth 值优先于 MaxWidth 值,而值又优先于 Width 值。 第四个属性 ActualWidth是只读的,它报告通过与布局过程的交互所确定的实际宽度。

以下可扩展应用程序标记语言(XAML)示例将 Rectangle 元素(rect1)绘制为 Canvas子级。 可以使用表示 Rectangle、ListBox和 MinWidth属性值的一系列 MaxWidth 元素来更改 Width 的宽度属性。 通过这种方式,每个属性的优先级会以可视化形式显示。

Set the Rectangle Width:

25

50

75

100

125

150

175

200

225

250

Set the Rectangle MinWidth:

25

50

75

100

125

150

175

200

225

250

Set the Rectangle MaxWidth:

25

50

75

100

125

150

175

200

225

250

以下后台代码示例处理 SelectionChanged 事件引发的事件。 每个自定义方法从 ListBox获取输入,将值分析为 Double,并将该值应用于指定的与宽度相关的属性。 宽度值也会转换为字符串,并写入各种 TextBlock 元素(所选 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;

}

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

有关完整示例,请参阅 宽度属性比较示例。

另请参阅

ListBox

FrameworkElement

ActualWidth

MaxWidth

MinWidth

Width

面板概述

设置元素的高度属性

宽度属性比较示例

【免费下载】 CAD坐标标注插件zbbz 使用说明
天津市儿童医院预约挂号攻略