65.9K
CodeProject 正在变化。 阅读更多。
Home

动态按钮控件(C# 代码或 XAML 代码)

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.63/5 (6投票s)

2009年6月8日

CPOL

3分钟阅读

viewsIcon

64644

downloadIcon

3392

动态按钮控件(C# 代码或 XAML 代码)

Article_src

引言

这是一个简单的按钮控件,通过 XAML 和 C# 代码创建。这些技术使用两种语言,XAML 或 C# 。 在两种情况下,功能是相同的,但声明是不同的。以下功能用于创建控件。

1. ResourceDictionary (资源字典)

ResourceDictionary 类通常是一个隐式集合元素,是多个 Resources 属性的对象元素值。

2. Style (样式)

样式最常被声明为 Resources 部分中的资源。由于样式是资源,它们遵循适用于所有资源的相同范围规则,因此声明样式的位置会影响其应用的位置。 例如,如果在应用程序定义 XAML 文件的根元素中声明样式,则该样式可以在应用程序中的任何位置使用。 如果您正在创建一个导航应用程序,并在应用程序的 XAML 文件之一中声明样式,则该样式只能在该 XAML 文件中使用。

3. ControlTemplate (控件模板)

ControlTemplate 允许您指定控件的可视结构。 控件作者可以定义默认的 ControlTemplate,应用程序作者可以重写 ControlTemplate 以重建控件的可视结构。

4. Triggers (触发器)

WPF 定义了对应于最终用户操作的属性,例如当用户将光标悬停在 UIElement 上时设置为 trueIsMouseOver 属性,或者 ContentElement 的相应 IsMouseOver 属性。 使用属性值(以及 Trigger 元素)表示最终用户操作,允许 WPF 样式基于这些最终用户操作更改属性值,所有这些都在标记中完成。

5. GradientBrush (渐变画刷)

LinearGradientBrush 使用线性渐变绘制区域。 线性渐变定义了沿一条线的渐变。 该线的端点由线性渐变的 StartPointEndPoint 属性定义。 LinearGradientBrush 画刷沿着这条线绘制其 GradientStops。 默认的线性渐变是对角线。 在默认情况下,线性渐变的 StartPoint 是 (0,0),即正在绘制区域的左上角,其 EndPoint 是 (1,1),即正在绘制区域的右下角。 结果渐变中的颜色沿对角线路径插值。

6. FrameworkElementFactory

支持创建模板。

C# 源代码

this.Content = "C#Code";

//-----------------------------------GlassBackgroundFill--------------------------------

LinearGradientBrush gradient_GlassBackgroundFill = new LinearGradientBrush();
gradient_GlassBackgroundFill.StartPoint = new Point(0, 0);
gradient_GlassBackgroundFill.EndPoint = new Point(0, 1);

GradientStop color_BackgroundFill = new GradientStop();
color_BackgroundFill.Offset = 0;
color_BackgroundFill.Color = (Color)ColorConverter.ConvertFromString("#ff4b58");

GradientStop color2_BackgroundFill = new GradientStop();
color2_BackgroundFill.Offset = 1;
color2_BackgroundFill.Color = (Color)ColorConverter.ConvertFromString("#a91e28");

gradient_GlassBackgroundFill.GradientStops.Add(color_BackgroundFill);
gradient_GlassBackgroundFill.GradientStops.Add(color2_BackgroundFill);

//------------------------------------HoverGlassBackgroundFill-------------------------

RadialGradientBrush gradient_HoverGlassBackgroundFill = new RadialGradientBrush();
gradient_HoverGlassBackgroundFill.Center = new Point(0.5, 0.5);

GradientStop color_HoverGlassBackgroundFill = new GradientStop();
color_HoverGlassBackgroundFill.Offset = 0;
color_HoverGlassBackgroundFill.Color = 
	(Color)ColorConverter.ConvertFromString("#a91e28");

GradientStop color2_HoverGlassBackgroundFill = new GradientStop();
color2_HoverGlassBackgroundFill.Offset = 1;
color2_HoverGlassBackgroundFill.Color = 
	(Color)ColorConverter.ConvertFromString("#ff4b58");

gradient_HoverGlassBackgroundFill.GradientStops.Add(color_HoverGlassBackgroundFill);
gradient_HoverGlassBackgroundFill.GradientStops.Add(color2_HoverGlassBackgroundFill);

//---------------------------------HighlightFill---------------------------------------

LinearGradientBrush highlightFill = new LinearGradientBrush();
highlightFill.StartPoint = new Point(0, 0);
highlightFill.EndPoint = new Point(0, 1);

GradientStop color_highlightFill = new GradientStop();
color_highlightFill.Offset = 0;
color_highlightFill.Color = (Color)ColorConverter.ConvertFromString("#FFFFFFFF");

GradientStop color2_highlightFill = new GradientStop();
color2_highlightFill.Offset = 1;
color2_highlightFill.Color = (Color)ColorConverter.ConvertFromString("#00000000");

highlightFill.GradientStops.Add(color_highlightFill);
highlightFill.GradientStops.Add(color2_highlightFill);

//---------------------------------BorderStroke---------------------------------------
LinearGradientBrush borderStroke = new LinearGradientBrush();
borderStroke.StartPoint = new Point(0, 0);
borderStroke.EndPoint = new Point(0, 1);

GradientStop color_borderStroke = new GradientStop();
color_borderStroke.Offset = 0;
color_borderStroke.Color = (Color)ColorConverter.ConvertFromString("#c7c7c7");

GradientStop color2_borderStroke = new GradientStop();
color2_borderStroke.Offset = 1;
color2_borderStroke.Color = (Color)ColorConverter.ConvertFromString("#b0b0b0");

borderStroke.GradientStops.Add(color_borderStroke);
borderStroke.GradientStops.Add(color2_borderStroke);

//------------------------------------------------------------------------------------

ControlTemplate template = new ControlTemplate(typeof(Button));

FrameworkElementFactory grid = new FrameworkElementFactory(typeof(Grid));
grid.SetValue(Grid.RenderTransformOriginProperty, new Point(0.5, 0.5));

//------------------------------------------------------------------------------------

FrameworkElementFactory ele_BorderStock = new FrameworkElementFactory(typeof(Border));
ele_BorderStock.Name = "Outline";
ele_BorderStock.SetValue(Border.CornerRadiusProperty, new CornerRadius(9.0));
ele_BorderStock.SetValue(Border.BackgroundProperty, Brushes.Red);
ele_BorderStock.SetValue(Border.BorderBrushProperty, borderStroke);
ele_BorderStock.SetValue(Border.BorderThicknessProperty, new Thickness(1));

FrameworkElementFactory ele_GlassBackground = 
		new FrameworkElementFactory(typeof(Rectangle));
ele_GlassBackground.Name = "GlassBackground";
ele_GlassBackground.SetValue(Rectangle.MarginProperty, new Thickness(1, 1, 2, 2));
ele_GlassBackground.SetValue(Rectangle.RadiusXProperty, 9.0);
ele_GlassBackground.SetValue(Rectangle.RadiusYProperty, 9.0);
ele_GlassBackground.SetValue(Rectangle.FillProperty, gradient_GlassBackgroundFill);

FrameworkElementFactory ele_Highlight = new FrameworkElementFactory(typeof(Rectangle));
ele_Highlight.SetValue(Rectangle.MarginProperty, new Thickness(1, 1, 2, 2));
ele_Highlight.SetValue(Rectangle.RadiusXProperty, 9.0);
ele_Highlight.SetValue(Rectangle.RadiusYProperty, 9.0);
ele_Highlight.SetValue(Rectangle.OpacityProperty, 1.0);
ele_Highlight.SetValue(Rectangle.FillProperty, highlightFill);

FrameworkElementFactory ContentPresenterAll =
	new FrameworkElementFactory(typeof(ContentPresenter));
ContentPresenterAll.SetValue
	(ContentPresenter.VerticalAlignmentProperty, VerticalAlignment.Center);
ContentPresenterAll.SetValue
	(ContentPresenter.HorizontalAlignmentProperty, HorizontalAlignment.Center);

grid.AppendChild(ele_BorderStock);
grid.AppendChild(ele_GlassBackground);
grid.AppendChild(ele_Highlight);
grid.AppendChild(ContentPresenterAll);

//---------------------------ControlTemplate.Triggers.IsMouseOver------------------------

Trigger tg_IsMouseOver = new Trigger();
tg_IsMouseOver.Property = IsMouseOverProperty;
tg_IsMouseOver.Value = true;

Setter str_IsMouseOver = new Setter();
str_IsMouseOver.TargetName = "GlassBackground";
str_IsMouseOver.Property = Rectangle.FillProperty;
str_IsMouseOver.Value = gradient_HoverGlassBackgroundFill;
tg_IsMouseOver.Setters.Add(str_IsMouseOver);

template.Triggers.Add(tg_IsMouseOver);

//------------------------ControlTemplate.Triggers.IsPressed-----------------------------

Trigger tg_IsPressed = new Trigger();
tg_IsPressed.Property = Button.IsPressedProperty;
tg_IsPressed.Value = true;

Setter str_IsPressed = new Setter();
str_IsPressed.TargetName = "GlassBackground";
str_IsPressed.Property = Rectangle.FillProperty;
str_IsPressed.Value = Brushes.Red;
tg_IsPressed.Setters.Add(str_IsPressed);

template.Triggers.Add(tg_IsPressed);
//---------------------------------------------------------------------------------------

template.VisualTree = grid;

Style newStyle = new Style();
newStyle.TargetType = typeof(Button);

Setter str_margin = new Setter(Button.MarginProperty, new Thickness(1.0));
Setter str_snapsToDevicePixes = new Setter(Button.SnapsToDevicePixelsProperty, true);
Setter str_OverridesDefaultStyle = 
	new Setter(Button.OverridesDefaultStyleProperty, true);
Setter str_minHeight = new Setter(Button.MinHeightProperty, 16.0);
Setter str_minWidth = new Setter(Button.MinWidthProperty, 16.0);
Setter str_fontFamiy = new Setter(Button.FontFamilyProperty, new FontFamily("Verdana"));
FontSizeConverter myFontSizeConverter = new FontSizeConverter();
Setter str_fontSize = new Setter
    (Button.FontSizeProperty, (Double)myFontSizeConverter.ConvertFromString("11px"));
Setter str_foreground = new Setter(Button.ForegroundProperty, Brushes.White);
Setter str_ModifiedTemplate = new Setter(TemplateProperty, template);

newStyle.Setters.Add(str_margin);
newStyle.Setters.Add(str_snapsToDevicePixes);
newStyle.Setters.Add(str_OverridesDefaultStyle);
newStyle.Setters.Add(str_minHeight);
newStyle.Setters.Add(str_minWidth);
newStyle.Setters.Add(str_fontFamiy);
newStyle.Setters.Add(str_fontSize);
newStyle.Setters.Add(str_foreground);
newStyle.Setters.Add(str_ModifiedTemplate);

this.Style = newStyle;

XAML 源代码

 <Style x:Key="GlassButton" TargetType="{x:Type Button}">
    <Setter Property="Margin" Value="1" />
    <Setter Property="SnapsToDevicePixels" Value="true"> </Setter>
    <Setter Property="OverridesDefaultStyle" Value="true"> </Setter>
    <Setter Property="MinHeight" Value="16"> </Setter>
    <Setter Property="MinWidth" Value="16"> </Setter>
    <Setter Property="FontFamily" Value="Verdana"> </Setter>
    <Setter Property="FontSize" Value="11px" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="Button" >
            <Grid x:Name="Content" RenderTransformOrigin="0.5,0.5">
            <Border Name="Outline" CornerRadius="9" 
			Background="{StaticResource ButtonPushedFill}"
               BorderBrush="{StaticResource BorderStroke}"
               BorderThickness="1" />
              <!-- Background -->
            <Rectangle x:Name="GlassBackground" 
			Margin="1,1,2,2" RadiusX="9" RadiusY="9"
             Fill="{StaticResource GlassBackgroundFill}">
            </Rectangle>
            <Rectangle x:Name="Highlight" Margin="1,1,2,2" RadiusX="9"
             RadiusY="9" Opacity="1" Fill="{StaticResource HighlightFill}">
               </Rectangle>
            <!-- This aligns the text in the center of the button -->
            <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
            </Grid>

            <!-- Triggers -->
            <ControlTemplate.Triggers>
               <Trigger Property="IsMouseOver" Value="true">
                  <Setter TargetName="GlassBackground" 
		Property="Fill" Value="{StaticResource HoverGlassBackgroundFill}" />
               </Trigger>
               <Trigger Property="IsPressed" Value="true">
                  <Setter TargetName="GlassBackground" 
		Property="Fill" Value="{StaticResource ButtonPushedFill}" />
               </Trigger>

            </ControlTemplate.Triggers>

           </ControlTemplate>
          </Setter.Value>
          </Setter>
          </Style>

关于作者

我于 2003 年开始了我的 IT 职业生涯,使用 C++ 和 VC++,我在 IT 行业获得了良好的经验。 最后,我转向 .NET 技术,并参与了许多 .NET 项目。 我在 .NET 项目的打包和部署方面有很好的经验。 我从事过图形和数字图像处理,并开发了一种用于图形模式下高速打印的算法,这是该项目的一大成功。 我用 C# 开发了自定义控件。 我还对 WPF 和 WCF 等最新技术有很好的了解,具有良好的学习技能并且对从事先进技术工作感兴趣。 目前我正在为 Wipro Technologies 工作。 我的爱好包括读书、游泳和听音乐。

© . All rights reserved.