















//D:\C\WpfApp10\Themes\Generic.xaml <Style TargetType="{x:Type local:NumUpDownScroller}" x:Key="NumUpDownScrollerStyle1"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:NumUpDownScroller}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Width="50" Height="150"> <StackPanel Orientation="Vertical" Margin="5"> <Button x:Name="PART_UpBtn" Content="Up"/> <TextBlock Text="{Binding Path=NumStr,RelativeSource={RelativeSource AncestorType={x:Type local:NumUpDownScroller}}}" HorizontalAlignment="Center"/> <Button x:Name="PART_DownBtn" Content="Down"/> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> //D:\C\WpfApp10\NumUpDownScroller.cs using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp10 { /// <summary> /// Follow steps 1a or 1b and then 2 to use this custom control in a XAML file. /// /// Step 1a) Using this custom control in a XAML file that exists in the current project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:WpfApp10" /// /// /// Step 1b) Using this custom control in a XAML file that exists in a different project. /// Add this XmlNamespace attribute to the root element of the markup file where it is /// to be used: /// /// xmlns:MyNamespace="clr-namespace:WpfApp10;assembly=WpfApp10" /// /// You will also need to add a project reference from the project where the XAML file lives /// to this project and Rebuild to avoid compilation errors: /// /// Right click on the target project in the Solution Explorer and /// "Add Reference"->"Projects"->[Browse to and select this project] /// /// /// Step 2) /// Go ahead and use your control in the XAML file. /// /// <MyNamespace:NumUpDownScroller/> /// /// </summary> public class NumUpDownScroller : Control { private Button upBtn, downBtn; static NumUpDownScroller() { DefaultStyleKeyProperty.OverrideMetadata(typeof(NumUpDownScroller), new FrameworkPropertyMetadata(typeof(NumUpDownScroller))); } private static int num = 0; public override void OnApplyTemplate() { base.OnApplyTemplate(); var tempUpBtn = GetTemplateChild("PART_UpBtn") as Button; if(tempUpBtn!=null) { upBtn = tempUpBtn; upBtn.Click += UpBtn_Click; } var tempDownBtn = GetTemplateChild("PART_DownBtn") as Button; if (tempDownBtn != null) { downBtn= tempDownBtn; downBtn.Click += DownBtn_Click; } } private void DownBtn_Click(object sender, RoutedEventArgs e) { if(--num<0) { num = 60; } NumStr = $"{num:D2}"; } private void UpBtn_Click(object sender, RoutedEventArgs e) { if(++num>60) { num = 0; } NumStr = $"{num:D2}"; } public string NumStr { get { return (string)GetValue(NumStrProperty); } set { SetValue(NumStrProperty, value); } } // Using a DependencyProperty as the backing store for NumStr. This enables animation, styling, binding, etc... public static readonly DependencyProperty NumStrProperty = DependencyProperty.Register(nameof(NumStr), typeof(string), typeof(NumUpDownScroller), new PropertyMetadata("0",OnNumStrChanged)); private static void OnNumStrChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { } } }
//D:\C\WpfApp10\App.xaml <Application x:Class="WpfApp10.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp10" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Themes/Generic.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> //D:\C\WpfApp10\MainWindow.xaml <local:NumUpDownScroller Grid.Row="1" Grid.Column="0" Style="{StaticResource NumUpDownScrollerStyle1}"/>



此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。