惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
雷峰网
雷峰网
博客园_首页
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
小众软件
小众软件
D
Docker
P
Proofpoint News Feed
B
Blog
Vercel News
Vercel News
B
Blog RSS Feed
U
Unit 42
月光博客
月光博客
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
Y
Y Combinator Blog
I
InfoQ
Recent Announcements
Recent Announcements

博客园 - FredGrit

WPF customize via three combied custom controls WPF DataGrid DataGridTemplateColumn DataTemplate ContentPresenter ContentTemplate WPF CustomControl override Template in Generic.xaml and invoke different style WPF HierarchicalDataTemplate customize via ToggleButton WPF DataGrid DataGridTemplateColumn DataTemplate call predefined DataTemplate via ContentPresenter and ContentTemplate WPF ListBox load data via contentcontrol an ItemTemplate WPF DataGrid load data from Asp.Net Core WebAPI WPF TreeView HierarchicalDataTemplate with grouped Data WPF display grouped data with GroupBox and ItemsControl WPF two listbox scroll syncchronously via behavior WPF invoke data from WebAPI,DataGridTemplate call pre defined DataTemplate via ContentPresenter WPF ListBox load data from WCF WPF datagrid load data from WCF via json, export selected items to json file WPF ItemsControl load data from WCF,DataTemplate, ContentControl WPF customize rotated wheel relentlessly via custom control WPF embed DataTemplate in HierchicalDataTemplate of TreeView WPF ContentControl, ItemsControl, ItemsPanelTemplate,VirtualizingStackPanel,convert xml string to List<T> WPF parse web.config recursively, TreeView and HierarchicalDataTemplate WPF Custom control in cs and Generic.xaml WPF ContextMenu independent visual tree resolved via Freezable implemented class WPF custom control GetTemplateChild vs FindName,NameScope separation between Logical Tree and Visual Tree, WPF ListBox ListView Datatemplate, parse xml to List via XmlSerializer and StringReader WPF TreeView HierarchicalDataTemplate, parse xml via traverse in XmlElement WPF parse xml via [XmRoot] and [XmlElement] attributes together, contentcontrol's template is ControlTemplate from Resources WPF ContentControl invoke Template from resource, reuse datatemplate WPF deserialize xml string as List via [XmlRoot] and [XmlElement] attribute WPF ContentControl Template WPF DatagridTemplate Binding DataTemplate WPF TreeView explicitly given key name to HierarchicalDataTemplate WPF, parse XMlDocument as List
WPF Customcontrol NumUpDownScroller
FredGrit · 2026-09-05 · via 博客园 - FredGrit
//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}"/>

image

image

image