如何在运行时更新 WPF 中图像的比例?
How can I update scale at runtime on Image in WPF?
我在 WPF 中动态更新图像的比例时遇到一些问题。我想要的确切行为是当我单击一个按钮时,我想放大(或缩小)UserControl 内的图像。
我的XAML:
<UserControl x:Class="Company.Scaling.ScaleControl"
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 x:Name="imageContainer" Stretch="None" Focusable="False" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image.LayoutTransform>
<ScaleTransform x:Name="scaleTransform">
</ScaleTransform>
</Image.LayoutTransform>
</Image>
</Grid>
我目前正在像这样更新属性 ScaleX 和 ScaleY:
this.scaleTransform.ScaleX = this.ZoomScale;
this.scaleTransform.ScaleY = this.ZoomScale;
当我在我的 XAML 的构造函数中更新这些时,它会像这样工作:
public ScaleControl()
{
this.InitializeComponent();
this.ZoomScale = 1.5f;
}
但是当我在运行时更新这些属性时(在我单击按钮之后)它不起作用。
我是不是漏掉了什么?
感谢您的帮助!
编辑:
根据 Clemens 所说的内容,我添加了一些内容。
XAML 中的绑定:
<Image.LayoutTransform>
<ScaleTransform
ScaleX="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}"
ScaleY="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Image.LayoutTransform>
依赖关系属性:
public static readonly DependencyProperty ZoomScaleProperty = DependencyProperty.Register("ZoomScale", typeof(double), typeof(ScaleControl));
和 属性:
public double ZoomScale
{
get { return (double)this.GetValue(ZoomScaleProperty); }
set { this.SetValue(ZoomScaleProperty, value); }
}
我是 WPF 的新手,所以也许我又遗漏了一些东西,但我不知道是什么。
设置 ZoomScale 属性 不会神奇地更新 ScaleTransform 的 ScaleX 和 ScaleY 属性,只是因为您之前已将 ZoomScale 分配给它们的值。
您必须绑定 ScaleTransform 属性到 ZoomScale,例如像这样:
<Image ...>
<Image.LayoutTransform>
<ScaleTransform
ScaleX="{Binding ZoomScale,
RelativeSource={RelativeSource AncestorType=UserControl}}"
ScaleY="{Binding ZoomScale,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Image.LayoutTransform>
</Image>
此外,ZoomScale 属性 必须通知值更改。在从 DependencyObject 派生的 class 中,您通常会将其声明为依赖项 属性,如下所示:
public static readonly DependencyProperty ZoomScaleProperty = DependencyProperty.Register(
"ZoomScale", typeof(double), typeof(ScaleControl));
public double ZoomScale
{
get { return (double)GetValue(ZoomScaleProperty); }
set { SetValue(ZoomScaleProperty, value); }
}
所以,我认为我添加了正确的内容:
public static readonly DependencyProperty ScalingProperty = DependencyProperty.Register("ZoomScale", typeof(float), typeof(UserControl));
和
public float ZoomScale { get { return (float)GetValue(ScalingProperty); } set { SetValue(ScalingProperty, value); } }
对于依赖项 属性。我还添加了:
ScaleX="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}"
在我的 XAML 中,但似乎没有任何东西可以缩放..
我在 WPF 中动态更新图像的比例时遇到一些问题。我想要的确切行为是当我单击一个按钮时,我想放大(或缩小)UserControl 内的图像。
我的XAML:
<UserControl x:Class="Company.Scaling.ScaleControl"
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 x:Name="imageContainer" Stretch="None" Focusable="False" HorizontalAlignment="Left" VerticalAlignment="Top">
<Image.LayoutTransform>
<ScaleTransform x:Name="scaleTransform">
</ScaleTransform>
</Image.LayoutTransform>
</Image>
</Grid>
我目前正在像这样更新属性 ScaleX 和 ScaleY:
this.scaleTransform.ScaleX = this.ZoomScale;
this.scaleTransform.ScaleY = this.ZoomScale;
当我在我的 XAML 的构造函数中更新这些时,它会像这样工作:
public ScaleControl()
{
this.InitializeComponent();
this.ZoomScale = 1.5f;
}
但是当我在运行时更新这些属性时(在我单击按钮之后)它不起作用。
我是不是漏掉了什么?
感谢您的帮助!
编辑:
根据 Clemens 所说的内容,我添加了一些内容。
XAML 中的绑定:
<Image.LayoutTransform>
<ScaleTransform
ScaleX="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}"
ScaleY="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Image.LayoutTransform>
依赖关系属性:
public static readonly DependencyProperty ZoomScaleProperty = DependencyProperty.Register("ZoomScale", typeof(double), typeof(ScaleControl));
和 属性:
public double ZoomScale
{
get { return (double)this.GetValue(ZoomScaleProperty); }
set { this.SetValue(ZoomScaleProperty, value); }
}
我是 WPF 的新手,所以也许我又遗漏了一些东西,但我不知道是什么。
设置 ZoomScale 属性 不会神奇地更新 ScaleTransform 的 ScaleX 和 ScaleY 属性,只是因为您之前已将 ZoomScale 分配给它们的值。
您必须绑定 ScaleTransform 属性到 ZoomScale,例如像这样:
<Image ...>
<Image.LayoutTransform>
<ScaleTransform
ScaleX="{Binding ZoomScale,
RelativeSource={RelativeSource AncestorType=UserControl}}"
ScaleY="{Binding ZoomScale,
RelativeSource={RelativeSource AncestorType=UserControl}}" />
</Image.LayoutTransform>
</Image>
此外,ZoomScale 属性 必须通知值更改。在从 DependencyObject 派生的 class 中,您通常会将其声明为依赖项 属性,如下所示:
public static readonly DependencyProperty ZoomScaleProperty = DependencyProperty.Register(
"ZoomScale", typeof(double), typeof(ScaleControl));
public double ZoomScale
{
get { return (double)GetValue(ZoomScaleProperty); }
set { SetValue(ZoomScaleProperty, value); }
}
所以,我认为我添加了正确的内容:
public static readonly DependencyProperty ScalingProperty = DependencyProperty.Register("ZoomScale", typeof(float), typeof(UserControl));
和
public float ZoomScale { get { return (float)GetValue(ScalingProperty); } set { SetValue(ScalingProperty, value); } }
对于依赖项 属性。我还添加了:
ScaleX="{Binding ZoomScale, RelativeSource={RelativeSource AncestorType=UserControl}}"
在我的 XAML 中,但似乎没有任何东西可以缩放..