简体   繁体   English

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

[英]Why does this WPF button stretch across the window?

The button below always expands to be as wide as the TextBlock. 下面的按钮总是扩展为与TextBlock一样宽。 I've tried StackPanel, DockPanel, Width="Auto", etc. 我试过StackPanel,DockPanel,Width =“Auto”等。

How can I make the button expand to the size of its own text (as in HTML) and not to the size of text in its environement? 如何使按钮扩展到其自己文本大小 (如在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>

ANSWER: 回答:

Thanks Greg, that did it. 谢谢Greg,做到了。 Here is the full XAML that works now, you can right-click the button to change its Content so see that the button expands and contracts appropriately. 以下是现在可以使用的完整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>

All you need to do is set the HorizontalAlignment property on your button. 您需要做的就是在按钮上设置Horizo​​ntalAlignment属性。 It defaults to stretch therefore filling the available space. 它默认为拉伸,因此填充可用空间。

<Button x:Name="ButtonFavorite"
        HorizontalAlignment="Left"
        Content="Customers" 
        Margin="10" 
        Width="Auto"
        Click="ButtonFavorite_Click">

Regarding your annoyance at the sizing of buttons, this is something that seems to be targeted at the designer in the designer/developer workflow, while you're clearly working on the developer portion. 关于你对按钮大小的烦恼,这似乎是设计师/开发人员工作流程中的设计师的目标,而你明确地在开发人员部分工作。 For the sake of development, I always apply a few styles in my App.xaml to ensure somewhat better button sizing. 为了开发起见,我总是在App.xaml中应用一些样式来确保更好的按钮大小。 For example, in the application tag in your app.xaml file: 例如,在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>

Regarding your actual question: 关于你的实际问题:

The problem is that your DockPanel is stretching to the width of the text and the button will naturally expand to fill the available area. 问题是您的DockPanel伸展到文本的宽度,按钮将自然扩展以填充可用区域。 If you want the quick and dirty solution you can do something like: 如果您想要快速而肮脏的解决方案,您可以执行以下操作:

<DockPanel HorizontalAlignment="Left">
    <Button x:Name="ButtonFavorite"
            DockPanel.Dock="Top"  
            Content="Customers" 
            Margin="10" 
            Width="Auto"
            MaxWidth="100"
            Click="ButtonFavorite_Click">
    </Button>
</DockPanel>

Note the MaxWidth. 注意MaxWidth。 If you want a more composable result, isolate your button in another panel. 如果您想要更可组合的结果,请在另一个面板中隔离您的按钮。 (I'm using a stackpanel because I believe someone else already used a grid in their example): (我正在使用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>

I like the StackPanel in this case because I find myself using it to create the horizontal "bar" of buttons along the bottom of a Form- err- Window in the right corner. 在这种情况下,我喜欢StackPanel,因为我发现自己使用它在右下角的Formr-Window底部创建了按钮的水平“条形”。

You could try isolating the button from the main panel by putting it in another panel. 您可以尝试通过将按钮放在另一个面板中来隔离主面板上的按钮。

<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>

As another method to do this: You could change the button's template so it's essentially wrapped in a centered StackPanel. 作为另一种方法:您可以更改按钮的模板,使其基本上包含在居中的StackPanel中。 Something like this: 像这样的东西:

<Button Content="Back">
    <Button.Template>
        <ControlTemplate>
            <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                <Button Content="{TemplateBinding Content}"></Button>
            </StackPanel>
        </ControlTemplate>
    </Button.Template>
</Button>

Or you could add a style to app.xaml (or any other place where you're storing your global styles) like this: 或者你可以添加一个样式到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>

Note that it's important to include the Style="{x:Null}" attribute on the button within the template if adding to the global styles, otherwise you'll get an infinite loop when it comes to rendering the button. 请注意,如果添加到全局样式,则在模板中的按钮上包含Style="{x:Null}"属性非常重要,否则在渲染按钮时会出现无限循环。

您可以将它们放在一个两列网格中,按钮只跨越一列,文本跨越两列吗?

Here's an example using a Grid layout versus a DockPanel. 这是使用Grid布局与DockPanel的示例。 The idea is to have 2 columns and 2 rows. 想法是有2列和2行。 Put the Button it a single cell and make that row/column pair auto-sizing. 将Button设置为单个单元格,并使该行/列对自动调整大小。 Then put the TextBox into the second row and have it span both of the columns. 然后将TextBox放入第二行并使其跨越两列。 This will essentially make the top-right cell just filler space and will achieve the behavior you're looking for. 这将使右上方的单元格成为填充空间,并将实现您正在寻找的行为。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM