为什么这个WPF按钮会拉伸到整个窗口?

Edw*_*uay 6 wpf xaml

下面的按钮总是扩展为与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>
Run Code Online (Sandbox Code Playgroud)

回答:

谢谢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>
Run Code Online (Sandbox Code Playgroud)

Bry*_*son 14

您需要做的就是在按钮上设置Horizo​​ntalAlignment属性.它默认为拉伸,因此填充可用空间.

<Button x:Name="ButtonFavorite"
        HorizontalAlignment="Left"
        Content="Customers" 
        Margin="10" 
        Width="Auto"
        Click="ButtonFavorite_Click">
Run Code Online (Sandbox Code Playgroud)

  • 这是一项设计决策,因此他们可以在所有文化中对待所有控制措施.当一些文化从右向左阅读时,他们是否应该将其保留为默认值?HTML是从更加以美国为中心的角度开发的. (2认同)

Gre*_*g D 8

关于您对按钮大小的烦恼,这似乎是设计师/开发人员工作流程中的设计师的目标,而您明确地在开发人员部分工作.为了开发起见,我总是在App.xaml中应用一些样式来确保更好的按钮大小.例如,在app.xaml文件的application标签中:

<Application.Resources>
  <Style TargetType="Button">
    <Setter Property="MinWidth" Value="60" />
    <Setter Property="MinHeight" Value="23" />
    <Setter Property="Margin" Value="3" />
  </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

关于你的实际问题:

问题是您的DockPanel伸展到文本的宽度,按钮将自然扩展以填充可用区域.如果您想要快速而肮脏的解决方案,您可以执行以下操作:

<DockPanel HorizontalAlignment="Left">
    <Button x:Name="ButtonFavorite"
            DockPanel.Dock="Top"  
            Content="Customers" 
            Margin="10" 
            Width="Auto"
            MaxWidth="100"
            Click="ButtonFavorite_Click">
    </Button>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)

注意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>
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我喜欢StackPanel,因为我发现自己使用它在右下角的Formr-Window底部创建了按钮的水平"条形".