相关文章推荐
咆哮的木瓜  ·  超赞!EMNLP2023 | ...·  1 年前    · 
完美的排球  ·  ConstraintLayout ...·  1 年前    · 
谦和的灌汤包  ·  Azure Active ...·  1 年前    · 

如果你要自定义一个图片按钮控件,那么如何在主窗体绑定这个控件上图片的Source呢?

我向大家介绍一个用 依赖属性(DependencyProperty) 实现的方法。

关于依赖属性的介绍,请大家参考: 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>

好了,现在这个基础的图片按钮控件就初步完成了。

在工程的主窗体添加这个控件

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

大家可以把图片的路径直接绑定给这个控件了!

【WPF】WPF开发用户控件、用户控件属性依赖DependencyProperty实现双向绑定、以及自定义实现Command双向绑定功能演示
Wpf开发过程中,最经常使用的功能之一,就是用户控件(UserControl)了。用户控件可以用于开发用户自己的控件进行使用,甚至可以用于打造一套属于自己的UI框架。依赖属性(DependencyProperty)是为用户控件提供可支持双向绑定的必备技巧之一,同样用处也非常广泛。
C# WPF Image Source 频繁切换导致垃圾回收不及时导致内存占用不断增加的问题
C# WPF Image Source 频繁切换导致垃圾回收不及时导致内存占用不断增加的问题
原文:WPF中通过代码定义模板 WPF中可以再XAML中定义模板,也可以通过C#代码定义模板,通过代码可能更清楚的看清其逻辑,而且代码的好处就是可以随时动态的去操作,而在XAML中定义的一般都是静态的。
1、后台代码相对路径添加(若为绝对路径,换UriKind的属性即可) BitmapImage testBitmapImage = new BitmapImage(new Uri(@"\bin\Sources\ON_btn_AutoDetect.
原文 WPF 3D:MeshGeometry3D的定义和光照 由于WPF计算光照会根据整个平面的方向向量,所以如果在不同面上使用同一个点可能会达到不同的光照效果。让我们用不同的定义Mesh的方法来演示这个问题。