下面的按钮总是扩展为与TextBlock一样宽。我尝试过StackPanel,DockPanel,Width =“Auto”等等。
如何将按钮扩展为自己文本的大小(如在HTML中),而不是扩展到其环境中文本的大小?
<DockPanel HorizontalAlignment="Left">
<Button x:Name="ButtonFavorite"
DockPanel.Dock="Top"
Content="Customers"
Margin="10"
Width="Auto"
Click="ButtonFavorite_Click">
</Button>
<TextBlock DockPanel.Dock="Top" Text="this is a long text which makes the button stretch across the window, if this text is just a couple words, the button will be smaller, and this drives me up the wall" Margin="10" TextWrapping="Wrap" />
</DockPanel>
谢谢Greg,做到了。以下是现在可以使用的完整XAML,您可以右键单击该按钮来更改其内容,以便看到该按钮可以适当地扩展和收缩。
<Window x:Class="Test3784234.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel HorizontalAlignment="Left">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" >
<Button x:Name="ButtonFavorite"
Padding="5"
Cursor="Hand"
DockPanel.Dock="Top"
Content="Customers"
Margin="10"
Click="ButtonFavorite_Click">
<Button.ContextMenu>
<ContextMenu>
<MenuItem x:Name="menuItemReports" Header="Reports" Click="MenuItem_Click" />
<MenuItem x:Name="menuItemContracts" Header="Contracts" Click="MenuItem_Click"/>
<MenuItem x:Name="menuItemCustomers" Header="Customers" Click="MenuItem_Click" />
<MenuItem x:Name="menuItemDocumentation" Header="Documentation Creation Instructions" Click="MenuItem_Click" />
<MenuItem x:Name="menuItemEmail" Header="E-Mail" Click="MenuItem_Click" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</StackPanel>
<TextBlock x:Name="TheMessage" DockPanel.Dock="Top" Text="Right-click the 'favorites' button to change its function." Margin="10" TextWrapping="Wrap"/>
</DockPanel>
</Window>
答案 0 :(得分:14)
您需要做的就是在按钮上设置HorizontalAlignment属性。它默认为拉伸,因此填充可用空间。
<Button x:Name="ButtonFavorite"
HorizontalAlignment="Left"
Content="Customers"
Margin="10"
Width="Auto"
Click="ButtonFavorite_Click">
答案 1 :(得分:8)
关于您对按钮大小的烦恼,这似乎是设计师/开发人员工作流程中设计人员的目标,而您明确地在开发人员部分工作。为了开发起见,我总是在App.xaml中应用一些样式来确保更好的按钮大小。例如,在app.xaml文件的应用程序标记中:
<Application.Resources>
<Style TargetType="Button">
<Setter Property="MinWidth" Value="60" />
<Setter Property="MinHeight" Value="23" />
<Setter Property="Margin" Value="3" />
</Style>
</Application.Resources>
关于你的实际问题:
问题是你的DockPanel伸展到文本的宽度,按钮会自然扩展以填充可用区域。如果您想要快速而肮脏的解决方案,您可以执行以下操作:
<DockPanel HorizontalAlignment="Left">
<Button x:Name="ButtonFavorite"
DockPanel.Dock="Top"
Content="Customers"
Margin="10"
Width="Auto"
MaxWidth="100"
Click="ButtonFavorite_Click">
</Button>
</DockPanel>
注意MaxWidth。如果您想要更可组合的结果,请在另一个面板中隔离您的按钮。 (我正在使用stackpanel,因为我相信其他人已经在他们的示例中使用了网格):
<DockPanel HorizontalAlignment="Left">
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
<Button x:Name="ButtonFavorite"
Content="Customers"
Margin="10"
Width="Auto"
Click="ButtonFavorite_Click" />
</StackPanel>
<TextBlock DockPanel.Dock="Top" Text="this is a long text which makes the button stretch across the window, if this text is just a couple words, the button will be smaller, and this drives me up the wall" Margin="10" TextWrapping="Wrap" />
</DockPanel>
在这种情况下,我喜欢StackPanel,因为我发现自己使用它来在右下角的Formr-Window底部创建按钮的水平“条形”。
答案 2 :(得分:2)
您可以尝试通过将按钮放在另一个面板中来隔离主面板上的按钮。
<DockPanel HorizontalAlignment="Left">
<Grid DockPanel.Dock="Top">
<Button x:Name="ButtonFavorite"
Content="Customers"
Margin="10"
Width="Auto"
Click="ButtonFavorite_Click">
</Button>
</Grid>
<TextBlock DockPanel.Dock="Top" Text="this is a long text which makes the button stretch across the window, if this text is just a couple words, the button will be smaller, and this drives me up the wall" Margin="10" TextWrapping="Wrap" />
</DockPanel>
答案 3 :(得分:0)
您可以将它们放在两列网格中,按钮只跨越一列,文本跨越两列吗?
答案 4 :(得分:0)
这是使用网格布局与DockPanel的示例。想法是有2列和2行。将Button设置为单个单元格,并使该行/列对自动调整大小。然后将TextBox放入第二行并使其跨越两列。这基本上会使右上角的单元格成为填充空间,并将实现您正在寻找的行为。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button
x:Name="ButtonFavorite"
Grid.Column="0"
Grid.Row="0"
Content="Customers"
Margin="10"
Width="Auto"
Click="ButtonFavorite_Click">
</Button>
<TextBlock
Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="1"
Margin="10"
TextWrapping="Wrap"
Text="this is a long text which makes the button stretch across the window, if this text is just a couple words, the button will be smaller, and this drives me up the wall" />
</Grid>
答案 5 :(得分:0)
作为另一种方法:您可以更改按钮的模板,使其基本上包含在居中的StackPanel中。像这样:
<Button Content="Back">
<Button.Template>
<ControlTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Content="{TemplateBinding Content}"></Button>
</StackPanel>
</ControlTemplate>
</Button.Template>
</Button>
或者您可以将样式添加到app.xaml(或您存储全局样式的任何其他位置),如下所示:
<Style TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<Button Style="{x:Null}" Content="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type Button}} }" FontWeight="Bold" Padding="5"></Button>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
请注意,如果添加到全局样式,则在模板中的按钮上包含Style="{x:Null}"
属性非常重要,否则在渲染按钮时会出现无限循环。