INotifyPropertyChanged,没有 UI 更新
我在网上寻找答案,但似乎无法让它发挥作用。这是我所拥有的:
public class UIValues : INotifyPropertyChanged
{
private double zoomValue = 1;
private static readonly UIValues instance = new UIValues();
public event PropertyChangedEventHandler PropertyChanged;
internal static UIValues Instance { get { return instance; } }
internal double ZoomValue
{
get { return zoomValue; }
set
{
if (this.zoomValue == value)
return;
this.zoomValue = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue"));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
}
然后我有这个:
<UserControl>
<UserControl.DataContext>
<local:UIValues x:Name="uiValues"/>
</UserControl.DataContext>
.
.
.
<Viewbox x:Name="vbViewBox" RenderTransformOrigin="0.5,0.5">
<local:ImageControl x:Name="imgControl" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Viewbox.RenderTransform>
<TransformGroup>
<CompositeTransform x:Name="trCompositeTransform" ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}" Rotation="0" SkewX="0" SkewY="0"/>
</TransformGroup>
</Viewbox.RenderTransform>
</Viewbox>
</UserControl>
所以基本上,每当我从代码隐藏的 UIValues 类中对 ZoomValue 进行更改时,UI 都不会更新。
有人知道为什么吗?
谢谢!
I looked around the web for an answer, but can't seem to get this to work. Here is what I have:
public class UIValues : INotifyPropertyChanged
{
private double zoomValue = 1;
private static readonly UIValues instance = new UIValues();
public event PropertyChangedEventHandler PropertyChanged;
internal static UIValues Instance { get { return instance; } }
internal double ZoomValue
{
get { return zoomValue; }
set
{
if (this.zoomValue == value)
return;
this.zoomValue = value;
this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue"));
}
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
}
and then I have this:
<UserControl>
<UserControl.DataContext>
<local:UIValues x:Name="uiValues"/>
</UserControl.DataContext>
.
.
.
<Viewbox x:Name="vbViewBox" RenderTransformOrigin="0.5,0.5">
<local:ImageControl x:Name="imgControl" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<Viewbox.RenderTransform>
<TransformGroup>
<CompositeTransform x:Name="trCompositeTransform" ScaleX="{Binding ZoomValue}" ScaleY="{Binding ZoomValue}" Rotation="0" SkewX="0" SkewY="0"/>
</TransformGroup>
</Viewbox.RenderTransform>
</Viewbox>
</UserControl>
So basically, whenever I make a change to the ZoomValue from the UIValues class from code-behind, the UI is not updated.
Anyone know why?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看发布的代码,我猜测您有类似的东西来更改缩放值。
问题是这个 xaml:-
构造了一个独立的 UIValues 实例,它与静态
Instance
属性返回的实例不同。因此,您将更改没有任何对象监听的对象的值。编辑
此外,ZoomLevel 是
内部
,因为它与绑定一起使用,因此必须是公共的。解决方案是使用
IApplicationService
并通过 App.xaml 执行操作。将您的类更改为:
将
UIValues
的实例添加到 App.Xaml:-然后像这样设置用户控件
DataContext
:-这就是说这是浪费
DataContext
您可能需要将其绑定到实际数据。您可以直接在绑定上指定Source
:-Looking at the posted code I'll have a guess that you have something like this to change the Zoom Value.
The problem is this xaml:-
constructs an independent instance of UIValues that is not the same instance returned by your static
Instance
property. Hence you will be changing a value on a object that nothing is listening to.Edit
Also ZoomLevel is
internal
, for it work with binding is must be public.The solution is to use
IApplicationService
and do things through App.xaml.Change you class to:
Add the instance of
UIValues
to App.Xaml:-Then set the user control
DataContext
like this:-That said this is waste of the
DataContext
which you may need to bind to real data. You can specify theSource
directly on the bindings instead:-将 ZoomValue 属性的访问修饰符更改为 public,一切都会正常工作。
Change access modifier for
ZoomValue
property to public and everything will work fine.