1
我有一个WPF窗口,上面有一个按钮。我希望按钮的宽度取决于文本的增加或减少。我怎样才能做到这一点?如何创建一个按钮,该按钮将根据文本的数量增加其宽度?
我有一个WPF窗口,上面有一个按钮。我希望按钮的宽度取决于文本的增加或减少。我怎样才能做到这一点?如何创建一个按钮,该按钮将根据文本的数量增加其宽度?
的Width
属性正好被设置为Auto
并给出一个的HorizontalAlignment(Center
,Right
或Left
),因为它的默认值是Stretch
,而这个事业填写其容器。
这里是它如何工作的一个例子:
CODE
<Window x:Class="StackOverflow.WPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="A Default Button"/>
<Button Content="An Auto Width Button"
Width="Auto"
HorizontalAlignment="Center"
Grid.Column="1"/>
<Button Content="An Auto Width and Height Button"
Width="Auto" Height="Auto"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Grid.Column="0" Grid.Row="1"/>
</Grid>
</Window>
输出
我你不^ T固定宽度设为您的按钮,它已经将其宽度调整为其内容。
你是否动态设置文本? – 2013-03-27 13:02:59
是的,它是动态设置的。 – DaveDev 2013-03-27 13:06:10