简体   繁体   English

用户控件中的图像属性

[英]Image property in usercontrol

I'd like to make a simple UserControl in WPF as follows : an image (let's say 128x128) with another image on it, smaller (48x48). 我想在WPF中制作一个简单的UserControl,如下所示:一个图像(比如说128x128),上面有另一个图像,较小(48x48)。 Let's call them imgMain and imgOverlay (the small one). 我们称它们为imgMain和imgOverlay(小的)。

Now I want to place my UserControl in any WPF app and be able to change the images and their size. 现在,我想将UserControl放在任何WPF应用程序中,并能够更改图像及其大小。 I put two properties in my usercontrol and was happy to see them show up in the right form in the VS2010 designer. 我在用户控件中放置了两个属性,很高兴看到它们在VS2010设计器中以正确的形式显示。 But when I try to put an image using the designer the control doesn't refresh and stays white. 但是,当我尝试使用设计器放置图像时,控件不会刷新并保持白色。 Same if I try to do it at runtime from code. 如果我尝试在运行时通过代码执行此操作,则相同。

I read several posts about DependencyProperties and all (something I'm not really familiar with) but they always seem to want to have more logic in the even of an image changing. 我阅读了几篇有关DependencyProperties以及所有内容的文章(我不太了解),但是即使在图像更改时,他们似乎总是希望拥有更多的逻辑。 I want something really simple and only visual. 我想要一些非常简单且只有视觉效果的东西。

Here is what I did for the properties : 这是我对属性所做的:

  • the size of the overlay 覆盖层的大小

     public Size OverlaySize { get { return new Size(imgOverlay.Width, imgOverlay.Height); } set { imgOverlay.Width = value.Width; imgOverlay.Height = value.Height; } } 
  • the Images for main and overlay 主图像和叠加图像

     public Image ImageMain { get { return imgMain; } set { imgMain = value; } } public Image ImageOverlay { get { return imgOverlay; } set { imgOverlay = value; } } 

You really do have to go and do the whole DependencyProperty dance. 您确实必须去做整个DependencyProperty舞蹈。 Sorry about that. 对于那个很抱歉。 WPF introduces its own event system, its own property system and makes you use that. WPF引入了自己的事件系统,自己的属性系统,并让您使用它。 I found it confusing at first. 一开始我感到困惑。 And then just plain tedious. 然后只是乏味。 Wait... I'll see if I can dig up some similar code for you... 等等...我看看是否可以为您挖掘一些类似的代码...

OK. 好。 This little class works as an Image control, that displays a different image depending on the value bound to it... 这个小类用作图像控件,根据绑定到它的值显示不同的图像...

The xaml code: 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>

And the code behind with the whole DependencyProperty dance... 以及整个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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM