国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

WPF自定義控件

這篇具有很好參考價值的文章主要介紹了WPF自定義控件。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

方式一:基于現(xiàn)有控件進(jìn)行擴(kuò)展,如基于button進(jìn)行擴(kuò)展,UI可直接用xmal進(jìn)行編輯設(shè)計,邏輯用xaml.cs進(jìn)行編輯

WPF自定義控件

方法二:直接創(chuàng)建wpf自定義控件

WPF自定義控件

本文用方法二開展自定義控件?。?!

1.自定義控件的內(nèi)容在代碼cs文件中,自定義控件繼承自Control,ui界面可在Genric.xaml中定義。

2.在Generic.xaml中定義控件界面

  <Style  TargetType="{x:Type ctrl:DevButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ctrl:DevButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">                       
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.1*" MaxWidth="5"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                //自定義控件中的組成 ,需要定義x:name,后臺代碼需要用到,button中的DevName是后臺cs中定義的依賴屬性
                                <Rectangle Margin="1" x:Name="statusLed"/>
                                <Button Grid.Column="1" x:Name="devBtn" Content="{TemplateBinding DevName}"/>
                            </Grid>
                        
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

?上述界面中定義了兩個控件,組成本自定義控件的外觀顯示。一個rectangle,用顏色對狀態(tài)進(jìn)行顯示,一個button,是本自定義控件的主要內(nèi)容,需要顯示設(shè)備名稱,Click事件/Command需要觸發(fā)任務(wù)。

3.后臺處理

3.1? 定義自定義屬性DevName

        public string DevName
        {
            get { return (string)GetValue(DevNameProperty); }
            set { SetValue(DevNameProperty, value); }
        }
        public static readonly DependencyProperty DevNameProperty =
        DependencyProperty.Register("DevName", typeof(string), typeof(DevButton), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnDevNameChanged)));
        private static void OnDevNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            DevButton ctrl =sender as DevButton;
            ctrl.DevName = e.NewValue.ToString();           
        }

3.2? 定義與前端界面UI元素對應(yīng)的信息

        private Rectangle statusLed;
        private Button devBtn;
        public override void OnApplyTemplate()
        {
            //備用方法 Template.FindName(DownButtonKey, this) as Button;
            statusLed = GetTemplateChild("statusLed") as Rectangle;
            devBtn = GetTemplateChild("devBtn") as Button;
            devBtn.Click += DevBtn_Click;
            base.OnApplyTemplate();                  
        }

依據(jù)控件名稱查找模板中的控件,并注冊button的click事件

3.3? 定義事件

       private void DevBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(DevName);
        }

自定義控件主要就是上述幾步??傮w代碼如下:


using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 WpfApp2
{
    /// <summary>
    /// Author:yut 2022-12-21
    /// Function:自定義控件,用于設(shè)備的啟??刂?,同時顯示設(shè)備的運行狀態(tài)
    /// <summary>
    public class DevButton : Control
    {        
     
        public DevButton()
        {
            SetCurrentValue(WidthProperty, 100d);
            SetCurrentValue(HeightProperty, 25d);
            SetCurrentValue(BackgroundProperty, Brushes.Yellow);            
        }
        static DevButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DevButton), new FrameworkPropertyMetadata(typeof(DevButton)));
        } 
        private Rectangle statusLed;
        private Button devBtn;
        public override void OnApplyTemplate()
        {            
            statusLed = GetTemplateChild("statusLed") as Rectangle;
            devBtn = GetTemplateChild("devBtn") as Button;
            devBtn.Click += DevBtn_Click;
            base.OnApplyTemplate();                  
        }

        #region 自定義屬性
        public int DevId
        {
            get { return (int)GetValue(DevIdProperty); }
            set { SetValue(DevIdProperty, value); }
        }
        public static readonly DependencyProperty DevIdProperty =
          DependencyProperty.Register("DevId", typeof(int), typeof(DevButton), new FrameworkPropertyMetadata(-1,new PropertyChangedCallback(OnDevIdChanged)));
        private static void OnDevIdChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            DevButton ctrl = (DevButton)sender;
            ctrl.DevId = (int)e.NewValue;
        }

        public string DevName
        {
            get { return (string)GetValue(DevNameProperty); }
            set { SetValue(DevNameProperty, value); }
        }
        public static readonly DependencyProperty DevNameProperty =
        DependencyProperty.Register("DevName", typeof(string), typeof(DevButton), new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnDevNameChanged)));
        private static void OnDevNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            DevButton ctrl =sender as DevButton;
            ctrl.DevName = e.NewValue.ToString();           
        }

        public int DevStatus
        {
            get { return (int)GetValue(DevStatusProperty); }
            set
            {
                SetValue(DevStatusProperty, value);              
            }
        }
        public static readonly DependencyProperty DevStatusProperty =
          DependencyProperty.Register("DevStatus", typeof(int), typeof(DevButton), new FrameworkPropertyMetadata(-1,new PropertyChangedCallback(OnDevStatusChanged)));
        private static void OnDevStatusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            DevButton ctrl = (DevButton)sender;
            ctrl.DevStatus = (int)e.NewValue; 
            ctrl.StatusBrush=(ctrl.DevStatus>0)?Brushes.Green:Brushes.LightGray;
        }

        public Brush StatusBrush
        {
            get { return (Brush)GetValue(StatusBrushProperty); }
            set
            {
                SetValue(StatusBrushProperty, value);
            }
        }
        public static readonly DependencyProperty StatusBrushProperty =
        DependencyProperty.Register("StatusBrush", typeof(Brush), typeof(DevButton), new FrameworkPropertyMetadata(Brushes.LightGray));

        #endregion


        private void DevBtn_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(DevName);          
         
        }

    }
}
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ctrl="clr-namespace:WpfApp2">
 


    <Style  TargetType="{x:Type ctrl:DevButton}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ctrl:DevButton}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                       
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="0.1*" MaxWidth="5"/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                            <!--Fill="{TemplateBinding DevStatus, Converter={StaticResource IntToBrushes}}"-->
                            <Rectangle Margin="1" x:Name="statusLed" Fill="{TemplateBinding StatusBrush}"/>
                                <Button Grid.Column="1" x:Name="devBtn" Content="{TemplateBinding DevName}"/>
                            </Grid>
                        
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <TextBlock Text="******************"/>
            <local:DevButton DevName="電機" DevStatus="2"/>
            <TextBlock Text="******************"/>
        </StackPanel>
    </Grid>
</Window>

運行效果如下:

WPF自定義控件

?文章來源地址http://www.zghlxwxcb.cn/news/detail-513253.html

到了這里,關(guān)于WPF自定義控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • WPF自定義按鈕控件

    WPF自定義按鈕控件

    在平時的WPF應(yīng)用中,系統(tǒng)提供的按鈕控件確實可以實現(xiàn)正常的邏輯,但是從視覺方面看的話,確實不夠美觀,而且每個項目的UI設(shè)計不盡相同。那么自定義按鈕控件就是必須的了,網(wǎng)上查找了很多自定義按鈕控件的辦法,但每次都是寫到一半就報錯。在參考了多個技術(shù)貼之后

    2024年02月08日
    瀏覽(22)
  • wpf 自定義combox控件

    wpf 自定義combox控件

    關(guān)鍵步驟 1、新建usercontrol使用基本的控件進(jìn)行設(shè)計 2、依賴屬性的定義,目的:外部調(diào)用時候能夠使用屬性進(jìn)行控件樣式的控制 例如 Width=\\\"200\\\" DisplayMemberPath=\\\"Name\\\" SelectedItem=\\\"{Binding SelectedItem,Mode=TwoWay}\\\" SelectionChanged=\\\"{Binding ProjectSelectCommand}\\\" CommandParameter=\\\"{Binding ElementName = Projec

    2024年02月09日
    瀏覽(22)
  • WPF grid控件定義行和列

    WPF grid控件定義行和列

    在此已經(jīng)學(xué)習(xí)了wpf Grid控件, WPF布局控件Grid的基本使用 - 使用kaxaml_bcbobo21cn的博客-CSDN博客 下面繼續(xù)學(xué)習(xí); 定義3行3列的基本代碼如下;為了看清效果,設(shè)置 ShowGridLines=\\\"True\\\"; ? 減少一列,效果如下; ? 只有行,沒有列; ? 指定第一列的寬度; ? 第一列指定寬度,剩下2列

    2024年02月13日
    瀏覽(28)
  • wpf 為自定義控件添加滾動條

    在WPF中為自定義控件添加滾動條通常涉及將自定義控件置于 ScrollViewer 控件內(nèi),并根據(jù)需要配置ScrollViewer的屬性。以下是一個基本步驟說明: 創(chuàng)建自定義控件 :首先,你有一個自定義控件(比如名為 RWrapPanel ,繼承自 WrapPanel 并實現(xiàn) IScrollInfo 接口以進(jìn)行平滑滾動管理)。 嵌

    2024年02月01日
    瀏覽(22)
  • wpf自定義控件-單/雙箭頭線

    using System; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; namespace CustomControls { [TypeDescriptionProvider(typeof(CustomTypeDescriptionProvider))] public class CustomArrow : Shape { public CustomArrow () { Stroke= new SolidColorBrush(Color.FromRgb(0, 140, 206));

    2024年02月15日
    瀏覽(19)
  • WPF自定義控件庫之Window窗口

    WPF自定義控件庫之Window窗口

    在WPF開發(fā)中,默認(rèn)控件的樣式常常無法滿足實際的應(yīng)用需求,我們通常都會采用引入第三方控件庫的方式來美化UI,使得應(yīng)用軟件的設(shè)計風(fēng)格更加統(tǒng)一。常用的WPF的UI控件庫主要有以下幾種,如: Modern UI for WPF , MaterialDesignInXamlToolkit ,PanuonUI,Newbeecoder.UI,WPF UI , AduSkin ,

    2024年02月08日
    瀏覽(32)
  • WPF 自定義控件完成庫容表盤顯示效果

    WPF 自定義控件完成庫容表盤顯示效果

    先看一下顯示效果: ?????? 需要注意的地方有以下幾點: 表盤的刻度分部,長刻度和短刻度顯示。 在數(shù)值80W時,需要更改刻度盤的顏色漸變。 在數(shù)值80W時,更改庫容總數(shù)背景的顯示,也是顏色漸變??潭缺P控件屬性定義: 刻度盤的定義: 設(shè)置刻度盤的style: 庫容總數(shù)背

    2024年02月16日
    瀏覽(23)
  • WPF自定義控件之ItemsControl魚眼效果

    WPF自定義控件之ItemsControl魚眼效果

    原理 先獲取鼠標(biāo)在控件中的坐標(biāo),在獲取其每一項相對于ItemsControl的坐標(biāo),然后計算每一項離當(dāng)前鼠標(biāo)的距離,在根據(jù)這個距離,對其每一項進(jìn)行適當(dāng)?shù)目s放 實現(xiàn) 創(chuàng)建一個類,命名為FishEyeItemsControl ? public class FishEyeItemsControl : ItemsControl ? 添加應(yīng)用魚眼效果方法(控制其控

    2024年02月04日
    瀏覽(42)
  • WPF自定義嵌入彈框控件,支持內(nèi)容標(biāo)題自定義

    最近為了實現(xiàn)WPF中彈框組件寫了一個小例子: 組件要求: 1.自定義標(biāo)題 2自定義標(biāo)題顏色 3提供關(guān)閉按鈕, 4.彈框內(nèi)容可由調(diào)用方自行嵌入 xaml代碼 UserControl x:Class=\\\"WpfApp1.Controls.CustomPopup\\\" ? ? ? ? ? ? ?xmlns=\\\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\\\" ? ? ? ? ? ? ?xmlns:x=\\\"

    2024年02月16日
    瀏覽(23)
  • WPF 自定義DataGrid控件樣式模板5個

    WPF 自定義DataGrid控件樣式模板5個

    樣式一: 樣式代碼: 初始化綁定數(shù)據(jù)C#代碼: 效果展示: 樣式二: 上面的代碼實現(xiàn)了隔行換色的效果,但是沒有鼠標(biāo)選中效果。另外有些用戶希望能夠進(jìn)行列頭拖動及排序。那么就需要做以下更改: 添加DataGridRow樣式: 在引用時,設(shè)置DataGrid的RowStyle=\\\"{StaticResource AlertCoun

    2023年04月27日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包