簡體   English   中英

更新模型后WPF視圖未更新位置

[英]WPF View not Updated position after update Model

我有下一個結構:主窗口

<Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Grid Margin="100 70 0 0">
        <local:RotateControl x:Name="rtc1" Panel.ZIndex="1" 
                             Width="{Binding ElementName=window,  Path=ActualHeight, Converter={StaticResource SizeConverter}}" 
                             Height="{Binding ElementName=window,  Path=ActualHeight, Converter={StaticResource SizeConverter}}" 
                             Radius="{Binding ElementName=window,  Path=ActualHeight, Converter={StaticResource RadiusConverter}}" 
                             Loaded="rtc1_Loaded" SizeChanged="rtc1_SizeChanged"/>
    </Grid>
    <StackPanel HorizontalAlignment="Right" Canvas.Right="80" Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="year" Width="40"></TextBox>
            <TextBox x:Name="month" Width="20"></TextBox>
            <TextBox x:Name="day" Width="20"></TextBox>
            <TextBox x:Name="hour" Width="20"></TextBox>
            <TextBox x:Name="min" Width="20"></TextBox>
            <TextBox x:Name="sec" Width="20"></TextBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <TextBox x:Name="lat" Width="80" Text="55.75"></TextBox>
            <TextBox x:Name="lng" Width="80" Text="37.583333"></TextBox>
        </StackPanel>
        <ComboBox x:Name="cbHSys" Width="300" SelectedIndex="0"></ComboBox>
        <Button x:Name="GatData" Click="GatData_Click">Get data</Button>
        <ScrollViewer>
            <Label x:Name="julday"></Label>
        </ScrollViewer>
    </StackPanel>
</Canvas>

自定義控件

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Astro" 
    xmlns:s="clr-namespace:System;assembly=mscorlib">

    <local:XLineCoordConverter x:Key="XLineCoordConverter" />
    <local:YLineCoordConverter x:Key="YLineCoordConverter" />

    <SolidColorBrush x:Key="Blue1Brush" Color="#e2edfa" />
    <SolidColorBrush x:Key="Blue2Brush" Color="#0080c0" />

    <Pen x:Key="BlackPen1" Thickness="1" Brush="Black"></Pen>
    <Pen x:Key="BluePen1" Thickness="0.1" Brush="{StaticResource Blue2Brush}"></Pen>
    <Style TargetType="{x:Type local:RotateControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:RotateControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid Background="Transparent">
                            <Line  X1="{Binding Path=Radius}" Y1="{Binding Path=Radius}" Stroke="White" StrokeThickness="1">
                                <Line.X2>
                                    <MultiBinding Converter="{StaticResource XLineCoordConverter}" ConverterParameter="1">
                                        <Binding Path="Radius"/>
                                        <Binding Path="House1"/>
                                        <Binding Path="House2"/>
                                    </MultiBinding>
                                </Line.X2>
                                <Line.Y2>
                                    <MultiBinding Converter="{StaticResource YLineCoordConverter}" ConverterParameter="1">
                                        <Binding Path="Radius"/>
                                        <Binding Path="House1"/>
                                        <Binding Path="House2"/>
                                    </MultiBinding>
                                </Line.Y2>
                            </Line>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

和型號:

public class CircleModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        double _radius;
        public double Radius
        {
            get
            {
                return _radius;
            }
            set
            {
                if (_radius == value) return;
                _radius = value;
                OnPropertyChanged("Radius");
            }
        }

        double _angel;
        public double Angel
        {
            get
            {
                return _angel;
            }
            set
            {
                if (_angel == value) return;
                _angel = value;
                OnPropertyChanged("Angel");
            }
        }
        double _house1;
        public double House1
        {
            get
            {
                return _house1;
            }
            set
            {
                if (_house1 == value) return;
                _house1 = value;
                OnPropertyChanged("House1");
            }
        }
        ...................
    }

並為更改數據編寫代碼:

private void GetData()
        {
            julday.Content = "";
            //SetCurDate();
            DateTime date = new DateTime(Convert.ToInt32(year.Text), Convert.ToInt32(month.Text), Convert.ToInt32(day.Text),
                                         Convert.ToInt32(hour.Text), Convert.ToInt32(min.Text), Convert.ToInt32(sec.Text));
            CalcNatalChart(date);
            _model.Radius = (this.ActualHeight - (this.ActualHeight > 150 ? 150 : 0)) / 2;
            this.ViewModel = _model;
        }

    private void GatData_Click(object sender, RoutedEventArgs e)
            {
                GetData();
            }    

    private void rtc1_Loaded(object sender, RoutedEventArgs e)
        {
            GetData();
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(1.0);
            _timer.Tick += timer_Tick;
            _timer.Start();
        }


        void timer_Tick(object sender, EventArgs e)
        {
            julday.Content = "";
            CalcNatalChart(DateTime.Now);
            _model.Radius = (this.ActualHeight - (this.ActualHeight > 150 ? 150 : 0)) / 2;
            this.ViewModel = _model;
        }

所有數據以正確的形式更新,但查看不正確。 僅當按下按鈕GatData_Click時。 動態更改數據不起作用。 您能幫我解決問題嗎? 謝謝

問題出在以下幾行:

_model.Radius = (this.ActualHeight - (this.ActualHeight > 150 ? 150 : 0)) / 2;
this.ViewModel = _model;

重新分配ViewModel后,視圖將不知道此更改,並使用舊的對象實例。 可以在構造函數中僅初始化一次ViewModel,然后更改其屬性,或者也可以使該ViewModel屬性可見,以便您可以在數據更改時通知。

試試這個...

而不是您的this.ViewModel = _model; this.ViewModel.Radious = _model.Radious;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM