C# 你能用按钮制作一个样式吗?就像图片中的按钮一样,在图片上用颜色标记文本?
我想知道是否有可能在WPF中制作一个按钮样式,其中您有一个文本、一个图像和图像上的文本(在图像上),并带有一个彩色标记,如下图所示 直到现在我才得到这个C# 你能用按钮制作一个样式吗?就像图片中的按钮一样,在图片上用颜色标记文本?,c#,.net,wpf,wpf-style,C#,.net,Wpf,Wpf Style,我想知道是否有可能在WPF中制作一个按钮样式,其中您有一个文本、一个图像和图像上的文本(在图像上),并带有一个彩色标记,如下图所示 直到现在我才得到这个 <Style TargetType="{x:Type Button}" x:Key="CatProBtn"> <Setter Property="Background" Value="#373737" /> <Setter Property="Foreground" Value="
<Style TargetType="{x:Type Button}" x:Key="CatProBtn">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="15" />
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<!--<Path x:Name="PathIcon" Width="15" Height="25" Stretch="Fill" Fill="#4C87B3" HorizontalAlignment="Left" Margin="17,0,0,0" Data="F1 M 30.0833,22.1667L 50.6665,37.6043L 50.6665,38.7918L 30.0833,53.8333L 30.0833,22.1667 Z "/>-->
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}"
HorizontalAlignment="Right" VerticalAlignment="Center"
/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDown">
<SoundPlayerAction Source="C:\Users\shaban\Downloads\Cash_register_beep_sound_.wav" />
</EventTrigger>
<Trigger Property="Button.IsPressed" Value="True">
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E59400" />
<Setter Property="Foreground" Value="White" />
<!--<Setter TargetName="PathIcon" Property="Fill" Value="Black" />-->
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="OrangeRed" />
<Setter Property="Foreground" Value="White"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Wheat"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
从按钮中的类似代码中获得灵感,并修改值以实现所需的结果 使用网格来布局内容。将价格叠加在图像上
<Button MinHeight="50" HorizontalAlignment="Center">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Title" TextAlignment="Center"/>
<!-- This is where your image is in your code -->
<TextBlock Grid.Row="1" Text="Your
image
here..." FontSize="14" Background="Gray" TextAlignment="Center"/>
<TextBlock Grid.Row="2" Text="Description Description Description" TextAlignment="Center" TextWrapping="Wrap" FontSize="8" MaxWidth="100"/>
<!-- Superimpose price on top of image with some opacity -->
<TextBlock Grid.Row="1" Text="20,00 kr." Background="Yellow" Foreground="Red" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,12" Opacity="0.6" FontSize="8"/>
</Grid>
</Button>
结果
从按钮中的类似代码中获得灵感,并修改值以实现所需的结果 使用网格来布局内容。将价格叠加在图像上
<Button MinHeight="50" HorizontalAlignment="Center">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="Title" TextAlignment="Center"/>
<!-- This is where your image is in your code -->
<TextBlock Grid.Row="1" Text="Your
image
here..." FontSize="14" Background="Gray" TextAlignment="Center"/>
<TextBlock Grid.Row="2" Text="Description Description Description" TextAlignment="Center" TextWrapping="Wrap" FontSize="8" MaxWidth="100"/>
<!-- Superimpose price on top of image with some opacity -->
<TextBlock Grid.Row="1" Text="20,00 kr." Background="Yellow" Foreground="Red" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,12" Opacity="0.6" FontSize="8"/>
</Grid>
</Button>
结果
您可以使用网格将一个控件覆盖到另一个控件上。它将类似于:
<Button>
<Grid>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center">
<TextBlock Text="Ispinde"/>
<Image Source="C:\Users\shaban\Pictures\Picture5.png"" Stretch="Uniform"/>
</StackPanel>
<Grid Width="100" Height="40" HorizontalAlignment="Right">
<Grid Background="Red" Opacity="0.6"/>
<TextBlock Foreground="White" Text="Overlay"/>
</Grid>
</Grid>
</Button>
可以使用网格将一个控件覆盖在另一个控件上。它将类似于:
<Button>
<Grid>
<StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Center">
<TextBlock Text="Ispinde"/>
<Image Source="C:\Users\shaban\Pictures\Picture5.png"" Stretch="Uniform"/>
</StackPanel>
<Grid Width="100" Height="40" HorizontalAlignment="Right">
<Grid Background="Red" Opacity="0.6"/>
<TextBlock Foreground="White" Text="Overlay"/>
</Grid>
</Grid>
</Button>
我需要做它作为一种风格,因为我必须做60个按钮自然。因此,修改您的样式和按钮控件,或从按钮派生的UserControl,以使用我演示的网格/覆盖模式。我只是向您展示了如何修改您提供的示例代码以获得所需的结果。我假设您的控件将具有Title、Image、Description、Price等属性,并且您将在代码中使用这些属性,在代码中使用绑定等(而不是硬编码的图像等)你展示了一个简单的例子,要求你对一个复杂的布局提供帮助。请看这里的答案,了解如何将你的风格和我的例子的布局方面结合起来。我对你的答案非常满意,我非常感谢你所需要的。我展示的代码只是为了测试,我将在运行时进行测试,而不是硬编码。非常感谢。我需要把它作为一种风格来做,因为我自然要花60个钮扣。因此,修改您的样式和按钮控件,或从按钮派生的UserControl,以使用我演示的网格/覆盖模式。我只是向您展示了如何修改您提供的示例代码以获得所需的结果。我假设您的控件将具有Title、Image、Description、Price等属性,并且您将在代码中使用这些属性,在代码中使用绑定等(而不是硬编码的图像等)你展示了一个简单的例子,要求你对一个复杂的布局提供帮助。请看这里的答案,了解如何将你的风格和我的例子的布局方面结合起来。我对你的答案非常满意,我非常感谢你所需要的。我展示的代码只是为了测试,我将在运行时进行测试,而不是硬编码。非常感谢你