Commit 739806e9 by liulongfei

可缩放图片控件

parent f1410da2
......@@ -13,6 +13,7 @@
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/HotkeyBox/HotkeyBox.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/GPIOPinTestControl/GPIOPinTestControl.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/ShowMessageControl/ShowMessageControl.xaml"></ResourceDictionary>
<ResourceDictionary Source="/VIZ.Framework.Common;component/Widgets/ResizeImageControl/ResizeImageControl.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
......@@ -182,6 +182,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Widgets\ResizeImageControl\ResizeImageControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Widgets\ShowMessageControl\ShowMessageControl.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
......@@ -320,6 +324,7 @@
<Compile Include="Widgets\NavigationControl\NavigationControl.cs" />
<Compile Include="Widgets\NavigationControl\NavigationItemControl.cs" />
<Compile Include="Widgets\NoneWindow\NoneWindow.cs" />
<Compile Include="Widgets\ResizeImageControl\ResizeImageControl.cs" />
<Compile Include="Widgets\ShowMessageControl\ShowMessage.cs" />
<Compile Include="Widgets\ShowMessageControl\ShowMessageControl.cs" />
<Compile Include="Widgets\TimeDisplayControl\TimeDisplayControl.cs" />
......
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace VIZ.Framework.Common
{
/// <summary>
/// 调整图片大小控件
/// </summary>
public class ResizeImageControl : Control
{
static ResizeImageControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ResizeImageControl), new FrameworkPropertyMetadata(typeof(ResizeImageControl)));
}
public ResizeImageControl()
{
this.SizeChanged += ResizeImageControl_SizeChanged;
}
#region ImageSource -- 图片源
/// <summary>
/// 图片源
/// </summary>
public ImageSource ImageSource
{
get { return (ImageSource)GetValue(ImageSourceProperty); }
set { SetValue(ImageSourceProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ImageSource. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ImageSourceProperty =
DependencyProperty.Register("ImageSource", typeof(ImageSource), typeof(ResizeImageControl), new PropertyMetadata(null));
#endregion
#region IconSource -- 图标源
/// <summary>
/// 图标源
/// </summary>
public ImageSource IconSource
{
get { return (ImageSource)GetValue(IconSourceProperty); }
set { SetValue(IconSourceProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for IconSource. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty IconSourceProperty =
DependencyProperty.Register("IconSource", typeof(ImageSource), typeof(ResizeImageControl), new PropertyMetadata(null));
#endregion
#region ShowImageMinWidth -- 显示图片的最小宽度
/// <summary>
/// 显示图片的最小宽度
/// </summary>
public double ShowImageMinWidth
{
get { return (double)GetValue(ShowImageMinWidthProperty); }
set { SetValue(ShowImageMinWidthProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ShowImageMinWidth. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ShowImageMinWidthProperty =
DependencyProperty.Register("ShowImageMinWidth", typeof(double), typeof(ResizeImageControl), new PropertyMetadata(20d));
#endregion
#region ShowImageMinHeight -- 显示图片的最小高度
/// <summary>
/// 显示图片的最小高度
/// </summary>
public double ShowImageMinHeight
{
get { return (double)GetValue(ShowImageMinHeightProperty); }
set { SetValue(ShowImageMinHeightProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ShowImageMinHeight. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ShowImageMinHeightProperty =
DependencyProperty.Register("ShowImageMinHeight", typeof(double), typeof(ResizeImageControl), new PropertyMetadata(20d));
#endregion
#region ImageVisibility -- 图片可见性
/// <summary>
/// 图片可见性
/// </summary>
public Visibility ImageVisibility
{
get { return (Visibility)GetValue(ImageVisibilityProperty); }
set { SetValue(ImageVisibilityProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for ImageVisibility. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty ImageVisibilityProperty =
DependencyProperty.Register("ImageVisibility", typeof(Visibility), typeof(ResizeImageControl), new PropertyMetadata(Visibility.Hidden));
#endregion
#region IconVisibility -- 图标可见性
/// <summary>
/// 图标可见性
/// </summary>
public Visibility IconVisibility
{
get { return (Visibility)GetValue(IconVisibilityProperty); }
set { SetValue(IconVisibilityProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for IconVisibility. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty IconVisibilityProperty =
DependencyProperty.Register("IconVisibility", typeof(Visibility), typeof(ResizeImageControl), new PropertyMetadata(Visibility.Hidden));
#endregion
#region AspectRatio -- 宽高比
/// <summary>
/// 宽高比
/// </summary>
public double AspectRatio
{
get { return (double)GetValue(AspectRatioProperty); }
set { SetValue(AspectRatioProperty, value); }
}
/// <summary>
/// Using a DependencyProperty as the backing store for AspectRatio. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty AspectRatioProperty =
DependencyProperty.Register("AspectRatio", typeof(double), typeof(ResizeImageControl), new PropertyMetadata(16d / 9d));
#endregion
/// <summary>
/// 大小改变时触发
/// </summary>
private void ResizeImageControl_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width < this.ShowImageMinWidth || e.NewSize.Height < this.ShowImageMinHeight)
{
this.ImageVisibility = Visibility.Hidden;
this.IconVisibility = Visibility.Visible;
}
else
{
this.ImageVisibility = Visibility.Visible;
this.IconVisibility = Visibility.Hidden;
}
this.Height = e.NewSize.Width / this.AspectRatio;
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:VIZ.Framework.Common">
<Style TargetType="{x:Type local:ResizeImageControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ResizeImageControl}">
<Grid Background="{TemplateBinding Background}">
<Image Source="{Binding Path=IconSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:ResizeImageControl}},Mode=OneWay}"
Visibility="{Binding Path=IconVisibility,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:ResizeImageControl}},Mode=OneWay}"
VerticalAlignment="Center" HorizontalAlignment="Center" Margin="3"></Image>
<Image Source="{Binding Path=ImageSource,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:ResizeImageControl}},Mode=OneWay}"
Visibility="{Binding Path=ImageVisibility,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type local:ResizeImageControl}},Mode=OneWay}"
VerticalAlignment="Center" HorizontalAlignment="Center" Width="20" Height="20"></Image>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment