gpt4 book ai didi

wpf - 自动调整大小按钮样式

转载 作者:行者123 更新时间:2023-12-02 17:35:42 24 4
gpt4 key购买 nike

我有这个样式:

<!--Normal Button Style -->
<Style TargetType="{x:Type Button}" x:Key="NormalButtonStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<Grid Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Effect="{DynamicResource ShadowEffect}" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="16"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" HorizontalAlignment="Left" Width="16" Stretch="None" Margin="0,1" />
<!--<TextBlock Grid.Column="1" Text="{TemplateBinding Content}" Width="Auto" Margin="0,1" Padding="0" TextWrapping="Wrap" VerticalAlignment="Center" />-->
<ContentPresenter Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

还有这个按钮:

<Button Content="Record" HorizontalAlignment="Left" 
VerticalAlignment="Top" Width="63" Style="{StaticResource NormalButtonStyle}"
Tag="Blabla.png" Height="24"/>

这给了我这个:

Button

问题

由于本地化原因,如何使按钮根据内部文本自动调整大小?

最佳答案

您的代码也有效。您必须仅从按钮中删除“Width=63”。

另一种方法 在这里,我使用 stackpanel 作为基于内容的堆栈面板大小,并忽略了有多少可用空间,如果有额外的可用空间,则在此处使用 TextWrapping。

 <Window.Resources>
<Style TargetType="{x:Type Button}" x:Key="NormalButtonStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ButtonBase}">
<StackPanel MinHeight="{TemplateBinding MinHeight}" Orientation="Horizontal" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" Effect="{DynamicResource ShadowEffect}" >
<Image VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" Margin="2,0,2,0" Width="16" Height="16" Stretch="None"/>
<TextBlock MaxWidth="{Binding Path=ActualWidth,RelativeSource={RelativeSource TemplatedParent}}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Text="{TemplateBinding Content}" TextWrapping="Wrap" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid ShowGridLines="True" Width="500">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<Button Content="R" HorizontalAlignment="Left" MinHeight="30" Padding="2,0,5,0" Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
<Button Content="Record" HorizontalAlignment="Left" Grid.Row="1" MinHeight="30" Padding="2,0,5,0" Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
<Button Content="Record here" HorizontalAlignment="Left" Grid.Row="2" MinHeight="30" Padding="2,0,5,0" Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
<Button Content="Record record record record Record record record record Record record record record Record record record record Record record record record Record record record record" Grid.Row="3" MinHeight="30" Padding="2,0,5,0" Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
<Button Content="Record record record record Record record record record Record record record record Record record record record Record record record record Record record record record" Grid.Row="4" MinHeight="30" Padding="2,0,5,0" Style="{StaticResource NormalButtonStyle}" Tag="btn1.jpg"/>
</Grid>

注意:

  1. 在样式中使用 MinHeight/MinWidth/MaxHeight/MaxWidth 属性designing.it 有助于本地化。

  2. 使用自动调整网格进行设计。

结果

enter image description here

关于wpf - 自动调整大小按钮样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205131/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com