您的问题似乎不完整,您是想询问关于C语言编程的某个具体问题吗?比如C语言的语法、编程技巧、项目开发等。请提供更具体的信息,这样我才能给出更准确的回答。
- 内容介绍
- 文章标签
- 相关推荐
本文共计870个文字,预计阅读时间需要4分钟。
本文介绍一种WPF自定义按钮的方法。实现效果包括:使用图片作为按钮背景;自定义鼠标悬停效果;自定义鼠标按下效果;自定义禁用效果;实现效果如图所示。
实现步骤:
1. 创建CustomButton.cs文件
2.添加以下代码:
csharp
using System.Windows;using System.Windows.Controls;using System.Windows.Media;public class CustomButton : Button{ public ImageSource BackgroundImage { get { return (ImageSource)GetValue(BackgroundImageProperty); } set { SetValue(BackgroundImageProperty, value); } }
public static readonly DependencyProperty BackgroundImageProperty=DependencyProperty.Register(BackgroundImage, typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(null));
public CustomButton() { this.MouseEnter +=CustomButton_MouseEnter; this.MouseLeave +=CustomButton_MouseLeave; this.MouseLeftButtonDown +=CustomButton_MouseLeftButtonDown; this.MouseLeftButtonUp +=CustomButton_MouseLeftButtonUp; this.IsEnabledChanged +=CustomButton_IsEnabledChanged; }
private void CustomButton_MouseEnter(object sender, MouseEventArgs e) { this.Background=new SolidColorBrush(Colors.LightGray); }
private void CustomButton_MouseLeave(object sender, MouseEventArgs e) { this.Background=null; }
private void CustomButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Background=new SolidColorBrush(Colors.DimGray); }
private void CustomButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.Background=null; }
private void CustomButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) { this.Background=null; } else { this.Background=new SolidColorBrush(Colors.LightGray); } }}
3. 在XAML中使用CustomButton:
xml
4. 替换your_image_path.png为你的图片路径。
现在,你已经有了一个自定义按钮,它具有自定义的背景图片、鼠标悬停、鼠标按下、禁用效果等。
本文介绍WPF一种自定义按钮的方法。
实现效果
- 使用图片做按钮背景;
- 自定义鼠标进入时效果;
- 自定义按压效果;
- 自定义禁用效果
实现效果如下图所示:
实现步骤
- 创建CustomButton.cs,继承自Button;
- 创建一个资源文件ButtonStyles.xaml;
- 在资源文件中设计按钮的Style;
- 在CustomButton.cs中添加Style中需要的依赖属性;
- 在程序中添加资源并引用(为了方便在不同的程序中引用自定义按钮,自定义按钮放在独立的类库中,应用程序中进行资源合并即可)。
示例代码
// ButtonStyles.xaml <Style x:Key="CustomButton" TargetType="{x:Type local:CustomButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomButton}"> <Grid x:Name="container"> <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Source="{Binding ButtonImage,RelativeSource={RelativeSource Mode=TemplatedParent}}"> <Image.RenderTransformOrigin> <Point X="0.5" Y="0.5"/> </Image.RenderTransformOrigin> <Image.RenderTransform> <ScaleTransform x:Name="scaletrans" ScaleX="1" ScaleY="1"/> </Image.RenderTransform> </Image> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" TargetName="container"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#2c000000" TargetName="container"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleX)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleY)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> // CustomButton.cs public class CustomButton : Button { public ImageSource ButtonImage { get { return (ImageSource)GetValue(ButtonImageProperty); } set { SetValue(ButtonImageProperty, value); } } public static readonly DependencyProperty ButtonImageProperty = DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(CustomButton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); } // App.xaml 合并资源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=".../ButtonStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> // view.xaml 使用 <Grid> <customcontrols:CustomButton Width="48" Height="48" Style="{StaticResource CustomButton}" ButtonImage="/Louzi.Paint;component/Images/Toolbar/write.png"/> </Grid>
以上就是C# WPF 自定义按钮的方法的详细内容,更多关于C# WPF 自定义按钮的资料请关注自由互联其它相关文章!
本文共计870个文字,预计阅读时间需要4分钟。
本文介绍一种WPF自定义按钮的方法。实现效果包括:使用图片作为按钮背景;自定义鼠标悬停效果;自定义鼠标按下效果;自定义禁用效果;实现效果如图所示。
实现步骤:
1. 创建CustomButton.cs文件
2.添加以下代码:
csharp
using System.Windows;using System.Windows.Controls;using System.Windows.Media;public class CustomButton : Button{ public ImageSource BackgroundImage { get { return (ImageSource)GetValue(BackgroundImageProperty); } set { SetValue(BackgroundImageProperty, value); } }
public static readonly DependencyProperty BackgroundImageProperty=DependencyProperty.Register(BackgroundImage, typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(null));
public CustomButton() { this.MouseEnter +=CustomButton_MouseEnter; this.MouseLeave +=CustomButton_MouseLeave; this.MouseLeftButtonDown +=CustomButton_MouseLeftButtonDown; this.MouseLeftButtonUp +=CustomButton_MouseLeftButtonUp; this.IsEnabledChanged +=CustomButton_IsEnabledChanged; }
private void CustomButton_MouseEnter(object sender, MouseEventArgs e) { this.Background=new SolidColorBrush(Colors.LightGray); }
private void CustomButton_MouseLeave(object sender, MouseEventArgs e) { this.Background=null; }
private void CustomButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { this.Background=new SolidColorBrush(Colors.DimGray); }
private void CustomButton_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { this.Background=null; }
private void CustomButton_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) { this.Background=null; } else { this.Background=new SolidColorBrush(Colors.LightGray); } }}
3. 在XAML中使用CustomButton:
xml
4. 替换your_image_path.png为你的图片路径。
现在,你已经有了一个自定义按钮,它具有自定义的背景图片、鼠标悬停、鼠标按下、禁用效果等。
本文介绍WPF一种自定义按钮的方法。
实现效果
- 使用图片做按钮背景;
- 自定义鼠标进入时效果;
- 自定义按压效果;
- 自定义禁用效果
实现效果如下图所示:
实现步骤
- 创建CustomButton.cs,继承自Button;
- 创建一个资源文件ButtonStyles.xaml;
- 在资源文件中设计按钮的Style;
- 在CustomButton.cs中添加Style中需要的依赖属性;
- 在程序中添加资源并引用(为了方便在不同的程序中引用自定义按钮,自定义按钮放在独立的类库中,应用程序中进行资源合并即可)。
示例代码
// ButtonStyles.xaml <Style x:Key="CustomButton" TargetType="{x:Type local:CustomButton}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomButton}"> <Grid x:Name="container"> <Image Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Source="{Binding ButtonImage,RelativeSource={RelativeSource Mode=TemplatedParent}}"> <Image.RenderTransformOrigin> <Point X="0.5" Y="0.5"/> </Image.RenderTransformOrigin> <Image.RenderTransform> <ScaleTransform x:Name="scaletrans" ScaleX="1" ScaleY="1"/> </Image.RenderTransform> </Image> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Opacity" Value="0.5" TargetName="container"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#2c000000" TargetName="container"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Trigger.EnterActions> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleX)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> <DoubleAnimation Storyboard.TargetName="scaletrans" Storyboard.TargetProperty="(ScaleTransform.ScaleY)" To="0.8" Duration="0:0:0.15" AutoReverse="True"/> </Storyboard> </BeginStoryboard> </Trigger.EnterActions> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> // CustomButton.cs public class CustomButton : Button { public ImageSource ButtonImage { get { return (ImageSource)GetValue(ButtonImageProperty); } set { SetValue(ButtonImageProperty, value); } } public static readonly DependencyProperty ButtonImageProperty = DependencyProperty.Register("ButtonImage", typeof(ImageSource), typeof(CustomButton), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender)); } // App.xaml 合并资源 <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=".../ButtonStyles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> // view.xaml 使用 <Grid> <customcontrols:CustomButton Width="48" Height="48" Style="{StaticResource CustomButton}" ButtonImage="/Louzi.Paint;component/Images/Toolbar/write.png"/> </Grid>
以上就是C# WPF 自定义按钮的方法的详细内容,更多关于C# WPF 自定义按钮的资料请关注自由互联其它相关文章!

