wpf 兩個自定義控制項

来源:https://www.cnblogs.com/njit-77/archive/2019/11/10/11831475.html
-Advertisement-
Play Games

wpf 兩個自定義控制項 一個是IP控制項,一個滑動條。先看下效果圖 IPControl 1、實際工作中有時需要設置IP信息,就想著做一個ip控制項。效果沒有window自帶的好,需要通過tab切換。但也能滿足使用。廢話不多說直接上代碼 IPControl.xaml IPControl.xaml.cs 2 ...


wpf 兩個自定義控制項

一個是IP控制項,一個滑動條。先看下效果圖

IPControl

1、實際工作中有時需要設置IP信息,就想著做一個ip控制項。效果沒有window自帶的好,需要通過tab切換。但也能滿足使用。廢話不多說直接上代碼

IPControl.xaml
<UserControl x:Class="WpfApp1.IPControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1">

    <Viewbox>
        <Border>
            <DockPanel>
                <DockPanel.Resources>
                    <Style TargetType="{x:Type TextBox}">
                        <Setter Property="HorizontalContentAlignment" Value="Center"/>
                        <Setter Property="VerticalContentAlignment" Value="Top"/>
                        <Setter Property="TextAlignment" Value="Center"/>
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="HorizontalAlignment" Value="Left"/>
                        <Setter Property="VerticalAlignment" Value="Stretch"/>
                        <Setter Property="MaxLength" Value="3"/>
                        <Setter Property="Width" Value="30"/>
                        <Setter Property="Height" Value="25"/>
                    </Style>
                    <Style TargetType="{x:Type Label}">
                        <Setter Property="Content" Value="."/>
                    </Style>
                </DockPanel.Resources>
                <TextBox TabIndex="1" GotFocus="TextBoxGotFocus" Text="{Binding Path=FirstIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/>
                <Label/>
                <TextBox TabIndex="2" GotFocus="TextBoxGotFocus" Text="{Binding Path=SecondIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/>
                <Label/>
                <TextBox TabIndex="3" GotFocus="TextBoxGotFocus" Text="{Binding Path=ThirdIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/>
                <Label/>
                <TextBox TabIndex="4" GotFocus="TextBoxGotFocus" Text="{Binding Path=ForthIPValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:IPControl}}, UpdateSourceTrigger=PropertyChanged}"/>
            </DockPanel>
        </Border>
    </Viewbox>
</UserControl>
IPControl.xaml.cs
    public partial class IPControl : UserControl
    {
        public IPControl()
        {
            InitializeComponent();
        }


        #region  DependencyProperty

        public static readonly DependencyProperty IPAddressProperty =
            DependencyProperty.Register("IPAddress", typeof(string), typeof(IPControl), new PropertyMetadata(defaultIP, (d, e) =>
            {
                if (d is IPControl control)
                {
                    control.UpdateParts(control);
                }
            }));

        public string IPAddress
        {
            get { return (string)GetValue(IPAddressProperty); }
            set { SetValue(IPAddressProperty, value); }
        }

        #endregion


        #region Static Field

        private static readonly string defaultIP = "127.0.0.1";

        #endregion


        #region Field

        private string firstIPValue;
        private string secondIPValue;
        private string thirdIPValue;
        private string forthIPValue;

        #endregion


        #region Property

        public string FirstIPValue
        {
            get { return firstIPValue; }
            set
            {
                if (firstIPValue != value)
                {
                    UpdateIPText(value, 1, ref firstIPValue);
                }
            }
        }

        public string SecondIPValue
        {
            get { return secondIPValue; }
            set
            {
                if (secondIPValue != value)
                {
                    UpdateIPText(value, 0, ref secondIPValue);
                }
            }
        }

        public string ThirdIPValue
        {
            get { return thirdIPValue; }
            set
            {
                if (thirdIPValue != value)
                {
                    UpdateIPText(value, 0, ref thirdIPValue);
                }
            }
        }

        public string ForthIPValue
        {
            get { return forthIPValue; }
            set
            {
                if (forthIPValue != value)
                {
                    UpdateIPText(value, 0, ref forthIPValue);
                }
            }
        }

        #endregion


        #region Private Method

        private void TextBoxGotFocus(object sender, RoutedEventArgs e)
        {
            InputMethod.Current.ImeState = InputMethodState.Off;
            TextBox tb = sender as TextBox;
            if (tb.Text.Length != 0)
            {
                tb.SelectAll();
            }
        }

        private void UpdateIPText(string oldvalue, int minValue, ref string newValue)
        {
            int.TryParse(oldvalue, out int iValue);
            if (iValue < minValue)
            {
                iValue = minValue;
            }
            if (iValue > 255)
            {
                iValue = 255;
            }
            newValue = iValue.ToString();
            IPAddress = GetIPAddress();
        }

        private string GetIPAddress()
        {
            string str = "";
            if (firstIPValue != null && firstIPValue.Length > 0)
            {
                str += firstIPValue + ".";
            }
            else
            {
                str += "0.";
            }
            if (secondIPValue != null && secondIPValue.Length > 0)
            {
                str += secondIPValue + ".";
            }
            else
            {
                str += "0.";
            }
            if (thirdIPValue != null && thirdIPValue.Length > 0)
            {
                str += thirdIPValue + ".";
            }
            else
            {
                str += "0.";
            }
            if (forthIPValue != null && forthIPValue.Length > 0)
            {
                str += forthIPValue;
            }
            else
            {
                str += "0";
            }
            return str;
        }

        private void UpdateParts(IPControl control)
        {
            if (control.IPAddress == null)
            {
                control.IPAddress = defaultIP;
            }
            string[] parts = control.IPAddress.Split('.');
            if (parts.Length == 4)
            {
                control.FirstIPValue = parts[0];
                control.SecondIPValue = parts[1];
                control.ThirdIPValue = parts[2];
                control.ForthIPValue = parts[3];
            }
        }

        #endregion

    }

2、控制項有4個TextBox、4個Label組成。TextBox顯示IP值,Label顯示IP數據的“.”。

TextBox綁定依賴屬性,設置TabIndex參數,通過Tab按鍵切換到下一個TextBox。每個TextBox最多輸入3位

LightControl

1、前段時間,領導緊急安排一個工作。做一個測試燈光的小軟體。與負責燈光同事溝通得知,光源板可同時控制24路燈。也就是說軟體界面上需要有24個ScrollBar用來表示燈光亮度,24個Label顯示名稱。這要是一個一個控制項加太慢了,沒法做一個自定義空間,可設置顯示名稱,通過滑動條或者直接設置參數,改變亮度。於是需要一個Label、一個ScrollBar、一個TextBox與ScrollBar關聯。

LightControl.xaml
<UserControl x:Class="WpfApp1.LightControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1">

    <Viewbox>
        <Border>
            <DockPanel>
                <Label Content="{Binding Path=LabelContent, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}}"
                       FontSize="17" Width="80" VerticalContentAlignment="Center"/>
                <ScrollBar Value="{Binding Path=LightValue, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}}"
                           Orientation="Horizontal" Height="40" Width="200" Maximum="100" SmallChange="1"/>
                <TextBox Text="{Binding Path=LightValue, StringFormat={}{0:F4}, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:LightControl}}, UpdateSourceTrigger=PropertyChanged}"
                         FontSize="17" Width="80" VerticalContentAlignment="Center"/>
            </DockPanel>
        </Border>
    </Viewbox>
</UserControl>
LightControl.xaml.cs
    public partial class LightControl : UserControl
    {
        public LightControl()
        {
            InitializeComponent();
        }


        #region  DependencyProperty

        public static readonly DependencyProperty LabelContentProperty =
            DependencyProperty.Register("LabelContent", typeof(string), typeof(LightControl), new PropertyMetadata("燈光"));

        public string LabelContent
        {
            get { return (string)GetValue(LabelContentProperty); }
            set { SetValue(LabelContentProperty, value); }
        }


        public static readonly DependencyProperty LightValueProperty =
            DependencyProperty.Register("LightValue", typeof(double), typeof(LightControl), new PropertyMetadata(1.0));

        public double LightValue
        {
            get { return (double)GetValue(LightValueProperty); }
            set { SetValue(LightValueProperty, value); }
        }

        #endregion


    }

2、Label顯示名稱通過依賴屬性由外接傳入。

3、ScrollBar的Value屬性與TextBox的Text屬性綁定同一個依賴屬性,可傳遞到調用者,同時TextBox顯示信息設置保留小數點4位。

工作中有時需要自己做一些自定義控制項,用來滿足不同場景的需求。兩個小控制項,比較簡單,希望此文能提供一些思路給你。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 你可能想創建一個在應用的任何地方都可以訪問的函數,這個教程將幫你實現 👏 很多教程都會說,你在 composer.json 這個文件中通過添加一個自動載入的文件,就可以實現這個需求。但我認為這不是一個好的方式,當你在 helpers.php 文件中添加了更多的函數時,可讀性將變得很差。 下麵我將介 ...
  • 在說正題之前先解釋一下交換機模式是個籠統的稱呼,它不是一個單獨的模式(包括了訂閱模式,路由模式和主題模式),交換機模式是一個比較常用的模式,主要是為了實現數據的同步。 首先,說一下訂閱模式,就和字面上的意思差不多主要就是一個生產者,多個消費者,同一個消息被多個消費者獲取,先看一下官網的圖示 整體執行 ...
  • 當創建隊列jobs、監聽器或訂閱伺服器以推送到隊列中時,您可能會開始認為,一旦分派,隊列工作器決定如何處理您的邏輯就完全由您自己決定了。 嗯……並不是說你不能從作業內部與隊列工作器交互,但是通常情況下,哪怕你做了,也是沒必要的。 這個神奇的騷操作的出現是因為“InteractsWithQueue”這 ...
  • 環境:MacOS 10.13 MAMAP Prophp 7.0.33 + xdebugVisual Studio Code前言我所理解的 POP Chain:利用魔術方法並巧妙構造特殊屬性調用一系列函數或類方法以執行某種敏感操作的調用堆棧反序列化常用魔法函數前言我所理解的 POP Chain:利用魔 ...
  • 【ASP.NET Core學習】在ASP.NET Core 種使用Entity Framework Core介紹,包括如何添加Entity Framwork Core,創建模型和遷移到資料庫,查詢數據,保存數據,使用事務,處理併發衝突 ...
  • [toc] 前言 在之前已經提到過,公用類庫Util已經開源,目的一是為了簡化開發的工作量,畢竟有些常規的功能類庫重覆率還是挺高的,二是為了一起探討學習軟體開發,用的人越多問題也就會越多,解決的問題越多功能也就越完善, 倉庫地址: "April.Util_github" , "April.Util_ ...
  • 在面對對象編程中,類的三大特性分別為封裝,繼承,多態。其中多態的具體實現,依賴於三個方法,也就是虛方法,抽象類和介面。 多態的具體作用是什麼呢?或者說多態的存在有什麼意義呢?多態的存在有效的降低了程式的耦合度,在使用的時候,不僅可以表現大家都有的共性,還能在必要的時候突出一些特殊的的個性。 那麼如何 ...
  • 近期和幾位做嵌入式開發的朋友閑聊過程中,一位朋友抱怨到:這C#太難用了,我想在N個窗體(或者是N個用戶組件之間)傳遞值都搞不定,非得要定義一個全局變數來存儲,然後用定時器來刷新值,太Low了。我急切的回答道:這很簡單,不就是委托的事嘛。那你來一個示例啊:朋友道。此為這篇博客的起因,所以此篇博客對於有 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...