1. 程式人生 > >WPF 使用者控制元件

WPF 使用者控制元件

         WPF中的使用者控制元件一般結合依賴屬性使用,從而讓使用者在使用控制元件的時候可以給控制元件的屬性自由賦值,下面就用一個簡單的自定義按鈕例項來說明其用法。

1、新建專案,選擇"WPF使用者控制元件庫"。

2、定義一個按鈕類,新建三個依賴屬性:ImageSourceNormal、ImageSourceMouseOver、ImageSourcePress,分別描述按鈕在正常狀態、滑鼠經過、滑鼠按下時顯示的圖片。

namespace MyControl
{
    /// <summary>
    /// 自定義按鈕的分部類
    /// </summary>
    partial class UserButton : UserControl
    {
        #region 正常狀態下顯示的背景圖片
        public ImageSource ImageSourceNormal
        {
            get { return (ImageSource)GetValue(ImageSourceNormalProperty); }
            set { SetValue(ImageSourceNormalProperty, value); }
        }

        public static readonly DependencyProperty ImageSourceNormalProperty =
            DependencyProperty.Register("ImageSourceNormal", typeof(ImageSource), typeof(UserButton), 
            new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/1.bmp"))));
        #endregion

        #region 滑鼠經過時顯示的背景圖片
        public ImageSource ImageSourceMouseOver
        {
            get { return (ImageSource)GetValue(ImageSourceMouseOverProperty); }
            set { SetValue(ImageSourceMouseOverProperty, value); }
        }

        public static readonly DependencyProperty ImageSourceMouseOverProperty =
            DependencyProperty.Register("ImageSourceMouseOver", typeof(ImageSource), typeof(UserButton), 
            new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/2.bmp"))));
        #endregion

        #region 滑鼠按下時顯示的背景圖片
        public ImageSource ImageSourcePress
        {
            get { return (ImageSource)GetValue(ImageSourcePressProperty); }
            set { SetValue(ImageSourcePressProperty, value); }
        }

        public static readonly DependencyProperty ImageSourcePressProperty =
            DependencyProperty.Register("ImageSourcePress", typeof(ImageSource), typeof(UserButton), 
            new PropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/MyControl;component/Images/3.bmp"))));
        #endregion
    }
}
3、在xaml檔案裡定義按鈕的模板樣式
<UserControl x:Class="MyControl.UserButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="23" Width="72">
    <Grid>
        <Button Width="72" Height="23">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="border" BorderBrush="Blue" BorderThickness="1" CornerRadius="3,3,3,3">
                        <Border.Background>
                            <ImageBrush ImageSource="{Binding ImageSourceNormal}"></ImageBrush>
                        </Border.Background>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="{Binding ImageSourceMouseOver}"></ImageBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background">
                                <Setter.Value>
                                    <ImageBrush ImageSource="{Binding ImageSourcePress}"></ImageBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</UserControl>
4、在cs檔案裡定義上下文
namespace MyControl
{
    /// <summary>
    /// UserButton.xaml 的互動邏輯
    /// </summary>
    public partial class UserButton : UserControl
    {
        public UserButton()
        {
            InitializeComponent();

            this.DataContext = this;
        }
    }
}

5、編譯使用者控制元件庫,生成MyControl.dll。

6、新建專案,選擇"WPF 應用程式"。

7、在xaml中引用MyControl名稱空間,注意得在引用名稱空間的字串里加上程式集的名稱

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="clr-namespace:MyControl;assembly=MyControl"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <mc:UserButton Margin="222,112,223,181"></mc:UserButton>
        <mc:UserButton ImageSourceNormal=".\Images\10.bmp"
                       ImageSourceMouseOver=".\Images\11.bmp"
                       ImageSourcePress=".\Images\12.bmp"
                       Margin="222,189,223,102"></mc:UserButton>
    </Grid>
</Window>

8、執行程式,就可以看到兩個使用者控制元件的效果,第一個顯示的預設屬性,第二個顯示的是指定的屬性。

相關推薦

C# WPF 歌詞控制元件(支援逐字定位描色效果)

原文: C# WPF 歌詞控制元件(支援逐字定位描色效果) 之前做了一個模仿網易雲歌詞的控制元件,實現了載入網易雲歌詞並能隨音樂播放進度定位歌詞。今天呢將在這個控制元件的基礎上增加逐字定位描色功能,如下圖效果(QQ音樂PC)所示:   我所使用的實現方法很簡單粗暴,把每句歌詞每個字切開,單

WPFのImage控制元件souce引入的方法總結

原文: WPFのImage控制元件souce引入的方法總結   1、後臺程式碼相對路徑新增(若為絕對路徑,換UriKind的屬性即可) BitmapImage testBitmapImage = new BitmapImage(new Uri(@"\bin\Sources\ON_bt

【C#】WPF音樂控制元件

一、使用SoundPlayer SoundPlayer 只能支援WAV格式的檔案 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.

wpf TabControl控制元件的SelectionChanged方法

對於老手來說很簡單,但是新手我從百度上找了好久沒找到,最後還是去谷歌找到的,哎,萬能的google。 前端介面: <TabControl Margin="0,10,0,0" Name="TabControls" SelectionChanged="TabControl_SelectionChang

C# wpf StackPanel控制元件和Border 控制元件 進行基本佈局(2)

1新建一個wpf 程式,去掉grid,控制元件,新增StackPanel控制元件,在新增4個button按鈕,如下圖所示, 分析程式碼,Orientation有2個屬性,1個屬性是Vertical 代表是縱向排列。HorizontalAlignment 代表縱向排列位置,有4個屬性,l

WPF 設定控制元件陰影后,引發的Y軸位置變化問題

背景 最近遇到一個動畫執行時,文字位置變化的問題。如下圖: 如果你仔細看的話,當星星變小時,文字往下降了幾個畫素。 貌似有點莫名其妙,因為控制元件之間並不在同一個Panel佈局控制元件中,不存在高度限制變化引發此類問題。所以有了如下測試 測試場景 1. 首先新建了一個空專案,前面是一個帶陰影的文

WPF判斷控制元件的滾動條是否移動到了最底部

/// <summary> /// Get a bool value indicate whether is the VerticalScrollBar at buttom /// </summary>

主視窗關閉時觸發子視窗的關閉事件 wpf使用者控制元件的關閉事件 wpf usercontrol 釋放時觸發關閉事件

/// <summary> /// 關閉視窗事件 /// </summary> /// <param name="sender"></param> /// <

WPF獲取控制元件位置的方法

WPF中提供了多種佈局方式,因此在佈局中的定位相對於WinForm的絕對定位要靈活的多,在WPF中,控制元件均沒有如WinForm中的Location屬性,但是,對應的提供了各種設定與獲取相對於承載元素的定位 一般來說,Wpf中的佈局控制元件大多都是相對定位(網格,流式,面板等),如果我們要改變控制元

WPF控制元件的ContextMenu屬性設定

效果 功能 1.使用Image、Label、Button等控制元件,實現單擊出現選單的樣式 2.將右鍵單擊顯示選單改為左鍵單擊顯示 程式碼 設計介面 <Image x:Name="ImMenu" Initialized="ImMenu_Initialize

WPF 使用者控制元件

         WPF中的使用者控制元件一般結合依賴屬性使用,從而讓使用者在使用控制元件的時候可以給控制元件的屬性自由賦值,下面就用一個簡單的自定義按鈕例項來說明其用法。 1、新建專案,選擇"WPF使用者控制元件庫"。 2、定義一個按鈕類,新建三個依賴屬性:ImageSo

一款開源免費的WPF圖表控制元件ModernuiCharts

一款簡潔好看的Chart控制元件  支援WPF、silverlight、Windows8  ,基本夠用,主要是開源免費的。(商業控制元件ComponentOne for WPF要4w多呢) This project provides a small library to display charts in 

WPF控制元件新增可以繫結的命令

在WPF裡的Button有一個可以繫結的Command的屬性,只要繫結好這個屬性以後,只要你ClickButton就 會執行這個命令,但這時我們可以考慮一下這個問題,為什麼是Click來觸發呢?為什麼不是右鍵單擊來觸發呢, 下面研究一下,怎麼能寫一個右鍵單機能觸發的命令:

WPF設定控制元件獲得焦點FocusManager

轉載地址:http://www.cnblogs.com/tommy-huang/p/5175948.html 簡單用法如下:     在父類容器中通過附加屬性FocusManager.FocusedElement來繫結需要強制獲得焦點的控制元件,用法如下:    

WPF實現控制元件拖動

實現控制元件拖動的基本原理是對滑鼠位置的捕獲,同時根據滑鼠按鍵的按下、釋放確定控制元件移動的幅度和時機。 簡單示例: 在Grid中有一個Button,通過滑鼠事件改編Button的Margin屬性,

一個好看的WPF開源控制元件庫——PanuonUI自定義樣式控制元件

PanuonUI 這是一個開源專案,版權歸本喵所有。該開源專案遵循Apache2.0許可協議,簡單點說就是不限制使用途徑(商業也可以用),但不允許抄襲。 GitHub傳送門:https://github.com/Ruris/PanuonUI 本開源庫目前仍

WPF自制控制元件如何定義依賴屬性

WPF系統封裝的控制元件基本可滿足使用,但有時為了美觀或功能的需求需要自定義控制元件,筆者以為自定義控制元件最重要在於如何定義依賴屬性,具體實施如下: 1)註冊屬性: public static readonly DependencyProperty XXXPropert

C# WPF DataGrid控制元件同行編輯的實時更新問題

    這些天一直在研究WPF,試圖用其來進行資料庫客戶端的製作。DataGrid控制元件以其資料顯示和實時編輯的方便易用,自然是不能不用。     資料庫程式中,往往要實現多級聯動這一功能以實現範圍

wpf控制元件字型大小智慧隨著內容長度的變化也變化。

在最近的工作中遇到一個需求,控制元件的fontsize 隨著 content的內容變化而自動適應大小。 整理了一下, 首先是2個方法。         /// <summary>         /// Measures the size of the tex

WPF Button控制元件模板

<Window x:Class="ControlTemplateDemo.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmln