更新:2007 年 11 月
获取或设置元素的最小宽度约束。这是一个依赖项属性。
命名空间:
System.Windows 程序集:
PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
<TypeConverterAttribute(GetType(LengthConverter))> _
<LocalizabilityAttribute(LocalizationCategory.None, Readability := Readability.Unreadable)> _
Public Property MinWidth As Double
Dim instance As FrameworkElement
Dim value As Double
value = instance.MinWidth
instance.MinWidth = value
[TypeConverterAttribute(typeof(LengthConverter))]
[LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable)]
public double MinWidth { get; set; }
[TypeConverterAttribute(typeof(LengthConverter))]
[LocalizabilityAttribute(LocalizationCategory::None, Readability = Readability::Unreadable)]
public:
property double MinWidth {
double get ();
void set (double value);
}
/** @property */
/** @attribute TypeConverterAttribute(LengthConverter) */
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable) */
public double get_MinWidth()
/** @property */
/** @attribute TypeConverterAttribute(LengthConverter) */
/** @attribute LocalizabilityAttribute(LocalizationCategory.None, Readability = Readability.Unreadable) */
public void set_MinWidth(double value)
public function get MinWidth () : double
public function set MinWidth (value : double)
<object MinWidth="double"/>
- or -
<object MinWidth="qualifiedDouble"/>
- double
Double
Double 值的字符串表示形式大于等于 0.0。它被解释为与设备无关的单位(1/96 英寸)度量值。字符串无需显式包含小数点。例如,值 1 是可接受的。
在“属性值”一节中提到的相同的 Double 范围限制适用。
- qualifiedDouble
如上所述的一个 double 值,后跟下列单位声明字符串之一:px、in、cm、pt。
px (默认值)为与设备无关的单位(每个单位 1/96 英寸)
in 表示英寸;1in==96px
cm 表示厘米;1cm==(96/2.54) px
pt 表示磅;1pt==(96/72) px
这是 FrameworkElement 上的三个用于指定宽度信息的属性之一。另外两个是 Width 和 MaxWidth。如果这三个值之间存在冲突,则应用程序确定宽度的实际顺序是:首先必须采用 MinWidth;然后采用 MaxWidth;最后,如果这些值中的每个值都在限制之内,则采用 Width。
Double 值上的限制是由 ValidateValueCallback 机制强制实施的。如果试图设置一个无效值,则会引发运行时异常。
本示例形象化地演示了 Windows Presentation Foundation (WPF) 中与宽度相关的四个属性之间的呈现行为差异。
FrameworkElement 类公开四个属性,用于描述某一元素的宽度特征。此四个属性可能相互冲突,如果相互冲突,则按如下顺序决定优先值:MinWidth 值优先于 MaxWidth 值,此值又优先于 Width 值。第四个属性 ActualWidth 是只读的。
以下可扩展应用程序标记语言 (XAML) 示例绘制了一个 Rectangle 元素 (rect1),作为 Canvas 的子元素。您可以通过使用一系列代表 MinWidth、MaxWidth 和 Width 属性值的列表框来更改 Rectangle 的宽度属性。通过这种方式,可以直观地显示每个属性的优先级。
<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>
下面的代码隐藏示例处理 SelectionChanged 事件引发的事件。每个自定义方法均从ListBox获取输入,将该值分析为Double,并将其应用于指定的宽度相关属性。宽度值还可转换为字符串,并写入到名为 txt1 的 TextBlock 元素。
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
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;
}
有关完整示例,请参见 宽度属性比较示例。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源