用户控件中的Image属性
本文关键字:Image 属性 控件 用户 | 更新日期: 2023-09-27 18:24:17
我想在WPF中制作一个简单的UserControl,如下所示:一个图像(假设为128x128),上面有另一个较小的图像(48x48)。让我们称它们为imgMain和imgOverlay(较小的一个)。
现在我想把我的UserControl放在任何WPF应用程序中,并能够更改图像及其大小。我在用户控件中放入了两个属性,很高兴看到它们在VS2010设计器中以正确的形式显示。但当我尝试使用设计器放置图像时,控件不会刷新,而是保持白色。如果我尝试在运行时从代码中执行,也是如此。
我读过几篇关于DependencyProperties之类的帖子(我并不太熟悉),但他们似乎总是希望在图像更改时有更多的逻辑。我想要一些非常简单,只有视觉效果的东西。
以下是我为属性所做的:
-
覆盖的大小
public Size OverlaySize { get { return new Size(imgOverlay.Width, imgOverlay.Height); } set { imgOverlay.Width = value.Width; imgOverlay.Height = value.Height; } }
-
主和覆盖的图像
public Image ImageMain { get { return imgMain; } set { imgMain = value; } } public Image ImageOverlay { get { return imgOverlay; } set { imgOverlay = value; } }
你真的必须去跳整个DependencyProperty
舞。很抱歉。WPF引入了它自己的事件系统,它自己的属性系统,并让您使用它。一开始我觉得很困惑。然后就是乏味乏味。等待我看看是否能为你找到一些类似的代码。。。
好的。这个小类用作图像控件,根据绑定的值显示不同的图像…
xaml代码:
<UserControl x:Class="DesignPerformanceViewer.Controls.BooleanImage"
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="300" d:DesignWidth="300">
<Grid>
<Image Name="image" Height="11" VerticalAlignment="Center" HorizontalAlignment="Center" SnapsToDevicePixels="True"/>
</Grid>
</UserControl>
和整个DependencyProperty
舞蹈背后的代码。。。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using System.ComponentModel;
namespace DesignPerformanceViewer.Controls
{
/// <summary>
/// Interaction logic for BooleanImage.xaml
/// </summary>
public partial class BooleanImage : UserControl
{
private static readonly DependencyProperty TrueSourceProperty = DependencyProperty.Register("TrueSource", typeof(ImageSource), typeof(BooleanImage));
private static readonly DependencyProperty TrueToolTipProperty = DependencyProperty.Register("TrueToolTip", typeof(string), typeof(BooleanImage));
private static readonly DependencyProperty FalseSourceProperty = DependencyProperty.Register("FalseSource", typeof(ImageSource), typeof(BooleanImage));
private static readonly DependencyProperty FalseToolTipProperty = DependencyProperty.Register("FalseToolTip", typeof(string), typeof(BooleanImage));
private static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(bool), typeof(BooleanImage),
new PropertyMetadata(false, new PropertyChangedCallback(OnValuePropertyChanged), new CoerceValueCallback(OnValueCoerceValueCallback)),
new ValidateValueCallback(OnValueValidateValueCallback));
public ImageSource TrueSource { get { return (ImageSource)GetValue(TrueSourceProperty); }
set
{
SetValue(TrueSourceProperty, value);
}
}
public ImageSource FalseSource { get { return (ImageSource)GetValue(FalseSourceProperty); } set { SetValue(FalseSourceProperty, value); } }
public string TrueToolTip { get { return (string)GetValue(TrueToolTipProperty); } set { SetValue(TrueToolTipProperty, value); } }
public string FalseToolTip { get { return (string)GetValue(FalseToolTipProperty); } set { SetValue(FalseToolTipProperty, value); } }
public bool Value
{
get { return (bool)GetValue(ValueProperty); }
set
{
SetValue(ValueProperty, value);
}
}
public BooleanImage()
{
InitializeComponent();
}
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var self = (BooleanImage) d;
var newValue = (bool) e.NewValue;
self.image.Source = newValue ? self.TrueSource : self.FalseSource;
self.ToolTip = newValue ? self.TrueToolTip : self.FalseToolTip;
}
private static object OnValueCoerceValueCallback(DependencyObject d, object baseValue)
{
if (!(baseValue is bool))
{
return false;
}
var boolValue = (bool) baseValue;
var self = (BooleanImage)d;
self.image.Source = boolValue ? self.TrueSource : self.FalseSource;
self.ToolTip = boolValue ? self.TrueToolTip : self.FalseToolTip;
return boolValue;
}
private static bool OnValueValidateValueCallback(object value)
{
return true;
}
}
}