WPF自定义控件的应用(DynamicResource的使用方法)

1 DynamicResource的使用方法

可以在字典文件 的抬头区写入数:

csharp 复制代码
 <SolidColorBrush x:Key="PrimaryBackgroundColor" Color="#FFABAdB3"/>
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
    <SolidColorBrush x:Key="TextSecundaryColor" Color="#FF569DE5"/>

设定好颜色:

则引用时直接使用:

csharp 复制代码
 BorderBrush="{DynamicResource PrimaryBackgroundColor}"

另一种方式 引用 fontFamily 的方式与此相似

csharp 复制代码
  <FontFamily x:Key="HgFont">../../CommonInMy/Fonts/#iconfont</FontFamily>

当然这个只是写在

节点下,如果想整个项目都用这个链接方法就是直接写在 app.xaml 下;

引用方式 FontFamily="{ DynamicResource HgFont}"

2 自定义控件(数据增减下拉操作)

2.1 控件本身类

csharp 复制代码
public class UInputNumberTextBox : Control
    { 
        static UInputNumberTextBox()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(UInputNumberTextBox), new FrameworkPropertyMetadata(typeof(UInputNumberTextBox)));
        }


        private Button btnSubtract;
        private Button btnAdd;


        public int Text
        {
            get { return (int)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Text.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(int), typeof(UInputNumberTextBox), new PropertyMetadata(0));


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            btnSubtract = GetTemplateChild("btnSubtract") as Button;
            if (btnSubtract != null)
            {
                btnSubtract.Click += BtnSubtract_Click;
            }

            btnAdd = GetTemplateChild("btnAdd") as Button;
            if (btnAdd != null)
            {
                btnAdd.Click += BtnAdd_Click;
            }
        }

        private void BtnAdd_Click(object sender, RoutedEventArgs e)
        {
            UInputNumberTextBox inputText = this as UInputNumberTextBox;
            inputText.Text += 1;
        }

        private void BtnSubtract_Click(object sender, RoutedEventArgs e)
        {
            UInputNumberTextBox inputText = this as UInputNumberTextBox;
            inputText.Text -= 1;
        }
    }

2.2 样式操作:

csharp 复制代码
<SolidColorBrush x:Key="PrimaryBackgroundColor" Color="#FFCFD6E5"/><!--定义颜色-->
    <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
    <SolidColorBrush x:Key="TextSecundaryColor" Color="#FF4E85BD"/>
    <!--<FontFamily x:Key="TextSecundaryColor" fa="#FF569DE5"/>-->

    <Style x:Key="ControNumberTB" TargetType="ControlSet:UInputNumberTextBox">
        <Setter Property="Background" Value="{DynamicResource PrimaryBackgroundColor}" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ControlSet:UInputNumberTextBox">

                    <Border
                        Width="{TemplateBinding Width}"
                        Height="{TemplateBinding Height}"
                        BorderBrush="{DynamicResource PrimaryBackgroundColor}"
                        BorderThickness="1">
                        <StackPanel Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Orientation="Horizontal" >

                            <Grid Width="{TemplateBinding Width}">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="2*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>

                                <Button
                                    x:Name="btnSubtract"
                                    Grid.Column="0"
                                    Width="{TemplateBinding Height}"
                                    Height="{TemplateBinding Height}"
                                    HorizontalAlignment="Left"
                                    VerticalAlignment="Center"
                                    Background="{DynamicResource PrimaryBackgroundColor}"
                                    BorderThickness="0,0,0,0"
                                    Content="&#xf068;"
                                    Cursor="Hand"
                                    FocusVisualStyle="{x:Null}"
                                    FontFamily="{DynamicResource  HgFont}"
                                    Foreground="{DynamicResource TextSecundaryColor}" /><!--减号-->

                                <TextBox
                                    Grid.Column="1"
                                    HorizontalAlignment="Center"
                                    VerticalAlignment="Center"
                                    Background="Transparent"
                                    BorderThickness="0"
                                    FocusVisualStyle="{x:Null}"
                                    FontSize="{TemplateBinding FontSize}"
                                    Foreground="{TemplateBinding Foreground}"
                                    Text="{Binding Text, RelativeSource={RelativeSource AncestorType=ControlSet:UInputNumberTextBox}, Mode=TwoWay}" />

                                <Button
                                    x:Name="btnAdd"
                                    Grid.Column="2"
                                    Width="{TemplateBinding Height}"
                                    Height="{TemplateBinding Height}"
                                    HorizontalAlignment="Right"
                                    VerticalAlignment="Center"
                                    Background="{DynamicResource PrimaryBackgroundColor}"
                                    BorderThickness="0,0,0,0"
                                    Content="&#xf067;"
                                    Cursor="Hand"
                                    FocusVisualStyle="{x:Null}"
                                    FontFamily="{ DynamicResource  HgFont}"
                                    Foreground="{DynamicResource TextSecundaryColor}" /> <!--加号-->
                            </Grid>
                        </StackPanel>
                    </Border>

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

3.引用在界面XAML中。

引用地址:

xmlns:自定义 = "clr-namespace:地址

csharp 复制代码
 xmlns:ControlSet="clr-namespace:海王牌位系统.CommonInMy.Controls"

引用

csharp 复制代码
  <ResourceDictionary Source="../../CommonInMy/Styles/UInputNumberTextBoxStyle.xaml"/>

界面定义,本关键字 加名称

csharp 复制代码
 <ControlSet:UInputNumberTextBox  Grid.Row="6" Grid.Column="1" Width="200"  Height="30"  Margin="0,0,0,10"  FontSize="16"  Style="{DynamicResource ControNumberTB }"  Text="1" />

本单元参考了

https://blog.csdn.net/hisxiwu/article/details/129886874

相关推荐
玉面小君1 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:Trigger、MultiTrigger、DataTrigger 的迁移
wpf·avalonia
招风的黑耳2 天前
Java生态圈核心组件深度解析:Spring技术栈与分布式系统实战
java·spring·wpf
lfw20192 天前
WPF 数据绑定模式详解(TwoWay、OneWay、OneTime、OneWayToSource、Default)
wpf
Magnum Lehar2 天前
3d wpf游戏引擎的导入文件功能c++的.h实现
3d·游戏引擎·wpf
FuckPatience3 天前
WPF Telerik.Windows.Controls.Data.PropertyGrid 自定义属性编辑器
wpf
almighty273 天前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
军训猫猫头4 天前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
我要打打代码4 天前
在WPF项目中使用阿里图标库iconfont
wpf
拾忆,想起5 天前
Redisson 分布式锁的实现原理
java·开发语言·分布式·后端·性能优化·wpf
weixin_464078075 天前
wpf依赖注入驱动的 MVVM实现(含免费源代码demo)
wpf