日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性

發(fā)布時間:2024/4/14 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原文:WPF 使用依賴屬性(DependencyProperty) 定義用戶控件中的Image Source屬性

如果你要自定義一個圖片按鈕控件,那么如何在主窗體綁定這個控件上圖片的Source呢?

我向大家介紹一個用 依賴屬性(DependencyProperty) 實(shí)現(xiàn)的方法。

關(guān)于依賴屬性的介紹,請大家參考:http://msdn.microsoft.com/zh-cn/library/ms752914.aspx

首先我們看用戶控件中如何定義這個依賴屬性:

1.新建一個用戶控件,命名為ImageButton

2.在CS定義如下代碼:

public partial class ImageButton : UserControl
??? {
??????? public ImageSource imgSource
??????? {
??????????? get { return (ImageSource)GetValue(ImageSourceProperty); }
??????????? set { SetValue(ImageSourceProperty, value); }
??????? }
??????? public static readonly DependencyProperty ImageSourceProperty;???????????
??????? public ImageButton()
??????? {
??????????? InitializeComponent();
??????? }
??????? static ImageButton()
??????? {
??????????? var metadata = new FrameworkPropertyMetadata((ImageSource)null);
??????????? ImageSourceProperty = DependencyProperty.RegisterAttached("imgSource", typeof(ImageSource), typeof(ImageButton), metadata);
??????? }
??? }

以上代碼,我們定義了控件中,Image 的Source屬性。

3.在控件的xaml中,添加一個Image控件

完整代碼如下:

<UserControl Name="UC" x:Class="TouchScreen12.Controls.ImageButton"
???????????? 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="167" d:DesignWidth="177">
??? <Grid x:Name="myGrid" Margin="0">
??????? <Image x:Name="myImage" Source="{Binding ElementName=UC, Path=imgSource}" Width="Auto" Height="Auto" Stretch="Fill" Margin="0,0,0,0"/>???
??? </Grid>
</UserControl>

好了,現(xiàn)在這個基礎(chǔ)的圖片按鈕控件就初步完成了。

在工程的主窗體添加這個控件

<imgBut:ImageButton Height="{Binding bHeight}" HorizontalAlignment="Center" x:Name="image1" VerticalAlignment="Center" Width="{Binding bWidth}" Margin="0" imgSource="{Binding Image1Path}"/>

大家可以把圖片的路徑直接綁定給這個控件了!

總結(jié)

以上是生活随笔為你收集整理的WPF 使用依赖属性(DependencyProperty) 定义用户控件中的Image Source属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。