开发者

INotifyPropertyChanged, no UI updates

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&开发者_StackOverflow中文版gt;
                    </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!


Looking at the posted code I'll have a guess that you have something like this to change the Zoom Value.

  UIValues.Instance.ZoomValue = x;

The problem is this xaml:-

  <local:UIValues x:Name="uiValues"/> 

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:

public class UIValues : INotifyPropertyChanged,  IApplicationService
{
    private double zoomValue = 1;

    public event PropertyChangedEventHandler PropertyChanged;

    internal static UIValues Instance { get; private set; }

    public double ZoomValue
    {
        get { return zoomValue; }
        set
        {
            if (zoomValue == value)
                return;

            zoomValue = value;
            this.OnPropertyChanged(new PropertyChangedEventArgs("ZoomValue"));
        }
    }

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, e);
    }

    void IApplicationService.StartService(ApplicationServiceContext context)
    {
        Instance = this;
        Application.Current.Resources.Add("UIValues", Instance);
    }

    void IApplicationService.StopService() { }
}

Add the instance of UIValues to App.Xaml:-

<Application.ApplicationLifetimeObjects>
    <local:UIValues />
</Application.ApplicationLifetimeObjects>

Then set the user control DataContext like this:-

<UserControl ... DataContext="{StaticResource UIValues}"> 

That said this is waste of the DataContext which you may need to bind to real data. You can specify the Source directly on the bindings instead:-

 <CompositeTransform ScaleX="{Binding ZoomValue, Source={StaticResource UIValues}}" ScaleY="{Binding ZoomValue, Source={StaticResource UIValues}}" Rotation="0" SkewX="0" SkewY="0"/>


Change access modifier for ZoomValue property to public and everything will work fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜