将 RenderTransform 元素绑定到代码隐藏变量
我正在遵循此线程中给出的答案: Access codebehind variable in XAML
这是我正在尝试执行的操作:
<Rectangle Name="MyRect" Fill="AliceBlue" MouseDown="Rectangle_MouseDown">
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform Angle="0" CenterX="300" CenterY="150"/>
<TranslateTransform X="{DynamicResource TransX}" Y="0"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
然后,我的代码中有一个变量,它的后面会发生变化。它称为 TransX,我将其添加为资源:
public double TransX = 0;
public SvgPreview()
{
InitializeComponent();
SvgPreview1.Resources.Add("TransX", TransX);
}
矩形从一开始就正确转换,但是转换不会重新渲染以反映 TransX 变量中的更改。我应该怎么办?
另外,我必须对其他几个值执行完全相同的操作。
I'm following the answers given in this thread here: Access codebehind variable in XAML
Here's what I'm trying to do:
<Rectangle Name="MyRect" Fill="AliceBlue" MouseDown="Rectangle_MouseDown">
<Rectangle.RenderTransform>
<TransformGroup>
<RotateTransform Angle="0" CenterX="300" CenterY="150"/>
<TranslateTransform X="{DynamicResource TransX}" Y="0"/>
</TransformGroup>
</Rectangle.RenderTransform>
</Rectangle>
Then, I have a variable in my code behind which gets changed. It's called TransX and I add it as a resource:
public double TransX = 0;
public SvgPreview()
{
InitializeComponent();
SvgPreview1.Resources.Add("TransX", TransX);
}
The rectangle does get transformed properly from the start, however the transforms are not re-rendered to reflect changes in the TransX variable. What should I do?
Also, I have to do this exact same thing for several other values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你应该让 SvgPreview 实现 INotifyPropertyChanged,将其设置为 Rectangle 的某个父级的 DataContext,并将 TransX 更改为属性并通过 DataBinding 访问它。
编辑:像这样:
在包含矩形的Windows/页面/控件的代码隐藏中;
I think you should make SvgPreview implement INotifyPropertyChanged, set it as the DataContext of some parent of your Rectangle, and change TransX into a property and access it via DataBinding.
Edit: Like this:
and in the CodeBehehind of the Windows/Page/Control that contains your Rectangle;