Nút dưới đây luôn mở rộng rộng bằng TextBlock. Tôi đã thử StackPanel, DockPanel, Chiều rộng = "Tự động", v.v.Tại sao nút WPF này trải dài trên cửa sổ?
Làm cách nào để làm cho nút mở rộng thành kích thước của văn bản riêng của mình (như trong HTML) và không theo kích thước văn bản trong môi trường?
<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>
ĐÁP:
Cảm ơn Greg, mà đã làm nó. Đây là XAML đầy đủ hoạt động ngay bây giờ, bạn có thể nhấp chuột phải vào nút để thay đổi Nội dung của nó để thấy rằng nút mở rộng và hợp đồng thích hợp.
<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>
Thú vị. Câu hỏi này nói có một câu trả lời nhiều hơn thực tế. –
Điều đó xảy ra nếu ai đó đăng lên rồi xóa câu trả lời. Trông giống như một công việc nhỏ của StackOverflow. –