我想在WPF中创建一个简单的UserControl,如下所示:一个图像(比方说128x128),上面有另一个图像,更小(48x48)。我们称它们为imgMain和imgOverlay(小的)。
现在我想将UserControl放在任何WPF应用程序中,并能够更改图像及其大小。我在我的usercontrol中放了两个属性,很高兴看到它们在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;
}
}
答案 0 :(得分:0)
你真的必须去做整个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;
}
}
}