WPF 关于C#代码实现ControlTemplate

现在主流的控件模板和样式是引用XAML资源,不过感觉没有c#代码实现那么灵活,现介绍一下代码实现ControlTemplate的方法:

// 控件呈现的显示内容1(这里为Image)
FrameworkElementFactoryfe = new FrameworkElementFactory( typeof (Image), " Image " );

BitmapImagebi
= new BitmapImage();
bi.BeginInit();
bi.UriSource
= new Uri( @" E:ChartControlHanYangChartControlImageMainBackground.jpg " );
bi.EndInit();

fe.SetValue(Image.SourceProperty,bi);

// 控件呈现的显示内容2(这里为TextBox)
FrameworkElementFactoryfe2 = new FrameworkElementFactory( typeof (TextBox), " TextBox " );
fe2.SetValue(TextBox.WidthProperty,
100.0 );
fe2.SetValue(TextBox.HeightProperty,
100.0 );

// 把要呈现的显示内容封装起来
FrameworkElementFactoryf = new FrameworkElementFactory( typeof (Grid), " Grid " );
f.AppendChild(fe);
f.AppendChild(fe2);

// 控件模板
ControlTemplatect = new ControlTemplate( typeof (Button));
ct.VisualTree
= f;

// 修改Button的Template
Buttonbtn = new Button();
btn.Template
= ct;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是制作温度计组件的详细步骤: 1. 创建一个新的WPF控件库项目。 2. 在项目中添加一个UserControl控件,命名为TemperatureControl。 3. 在TemperatureControl控件中添加一个Grid控件,并设置行和列的大小和数量,以便能够放置温度计的各个部分。 4. 在Grid控件中添加一个Ellipse控件,作为温度计的外框,设置其背景色和边框。 5. 添加一个TextBlock控件,用于显示当前温度值,将其放置在外框的中央。 6. 添加一个Rectangle控件,作为温度计的指针,设置其背景色和边框,并将其放置在外框的中央(稍微偏上一些)。 7. 添加一个依赖属性,用于设置当前温度值。 8. 在TemperatureControl控件的代码中,编写一个方法,用于将当前温度值转换为指针应该旋转的角度,并将指针旋转到该角度。 9. 在TemperatureControl控件的代码中,编写一个PropertyChangedCallback方法,用于在温度值改变时更新指针的角度。 10. 在TemperatureControl控件的代码中,添加一些样式和模板,用于美化控件。 完成上述步骤后,你可以将TemperatureControl控件添加到你的应用程序中,并设置温度值。 下面是实现细节: 1. 在TemperatureControl控件中添加以下XAML代码: ```xml <UserControl x:Class="WpfApp1.TemperatureControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"> <Grid> <Ellipse x:Name="OuterCircle" Stroke="Black" StrokeThickness="4" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBlock x:Name="TemperatureText" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40"/> <Rectangle x:Name="Pointer" Stroke="Black" StrokeThickness="4" Fill="Red" HorizontalAlignment="Center" VerticalAlignment="Center" Width="20" Height="120" RenderTransformOrigin="0.5,1.0"> <Rectangle.RenderTransform> <RotateTransform x:Name="PointerRotation"/> </Rectangle.RenderTransform> </Rectangle> </Grid> </UserControl> ``` 2. 添加一个依赖属性,用于设置当前温度值。在TemperatureControl控件的代码中添加以下代码: ```csharp public static readonly DependencyProperty TemperatureProperty = DependencyProperty.Register("Temperature", typeof(double), typeof(TemperatureControl), new PropertyMetadata(0.0, OnTemperatureChanged)); public double Temperature { get { return (double)GetValue(TemperatureProperty); } set { SetValue(TemperatureProperty, value); } } private static void OnTemperatureChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { TemperatureControl control = sender as TemperatureControl; control.UpdatePointer(); } ``` 3. 编写一个方法,用于将当前温度值转换为指针应该旋转的角度,并将指针旋转到该角度。在TemperatureControl控件的代码中添加以下代码: ```csharp private void UpdatePointer() { double angle = (Temperature - 50) * 1.8; PointerRotation.Angle = angle; TemperatureText.Text = Temperature.ToString("0") + "°"; } ``` 4. 编写一个PropertyChangedCallback方法,用于在温度值改变时更新指针的角度。在TemperatureControl控件的代码中添加以下代码: ```csharp private static void OnTemperatureChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { TemperatureControl control = sender as TemperatureControl; control.UpdatePointer(); } ``` 5. 为温度计组件添加样式和模板,以便美化控件。在TemperatureControl控件的代码中添加以下代码: ```xml <UserControl.Resources> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Black"/> <Setter Property="FontFamily" Value="Arial"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="HorizontalAlignment" Value="Center"/> </Style> <Style TargetType="Ellipse"> <Setter Property="Stroke" Value="Black"/> <Setter Property="StrokeThickness" Value="4"/> <Setter Property="Fill" Value="White"/> <Style.Triggers> <DataTrigger Binding="{Binding Temperature}" Value="100"> <Setter Property="Fill" Value="Red"/> </DataTrigger> </Style.Triggers> </Style> <Style TargetType="Rectangle"> <Setter Property="Stroke" Value="Black"/> <Setter Property="StrokeThickness" Value="4"/> <Setter Property="Fill" Value="Red"/> </Style> <ControlTemplate TargetType="local:TemperatureControl"> <Grid> <Ellipse x:Name="OuterCircle" Stroke="Black" StrokeThickness="4" Fill="White" HorizontalAlignment="Center" VerticalAlignment="Center"/> <TextBlock x:Name="TemperatureText" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="40"/> <Rectangle x:Name="Pointer" Stroke="Black" StrokeThickness="4" Fill="Red" HorizontalAlignment="Center" VerticalAlignment="Center" Width="20" Height="120" RenderTransformOrigin="0.5,1.0"> <Rectangle.RenderTransform> <RotateTransform x:Name="PointerRotation"/> </Rectangle.RenderTransform> </Rectangle> </Grid> </ControlTemplate> </UserControl.Resources> ``` 完成以上步骤后,你可以在应用程序中使用TemperatureControl控件,并通过设置Temperature属性来显示温度值。希望这些步骤能够帮到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值