2014-06-12 103 views
0

我有2個文本字段代表XminXmax四邊形的值,此值初始化的構造函數爲myClass更新2個字段取決於每個其他值,但每個也取決於其他屬性值

我也有2個領域,DeltaCounter,這取決於XminXmax他們必須使用公式更新:

this.Delta = (int)((this.Xmax - this.Xmin)/(this.Counter - 1)); 

this.Counter = ((this.Xmax - this.Xmin)/(this.Delta)) + 1 ;  

這似乎不是什麼難事,但是我想允許DeltaCounter更新,如果其他更改,即如果Delta更改然後Counter更新ES,反之亦然,這一切 考慮XminXmax

這裏是我有

WPF

<GroupBox Header="Values" Height="93" HorizontalAlignment="Left" Margin="4,3,0,0" Name="GBoxGridDefinition" VerticalAlignment="Top" Width="624"> 
<Grid> 
    <TextBlock Height="20" Margin="20,13,0,0" Name="TbXmin" Text="Xmin:" Width="36" />       
    <controls:WaterMarkNumericInput x:Name="TBXmin" Height="20" Width="89" Margin="59,9,0,0" Style="{StaticResource goStRWMNI}" Number="{Binding Path=Xmin, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}" IsNullAllowed="False" /> 
    <TextBlock Height="20" Margin="154,10,0,0" Name="TbXmax" Text="Xmax:" Width="36" />       
    <controls:WaterMarkNumericInput x:Name="TBXmax" Height="20" Width="89" Margin="198,10,0,0" Style="{StaticResource goStRWMNI}" Number="{Binding Path=Xmax, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}" IsNullAllowed="False" /> 
</Grid> 
</GroupBox> 
<GroupBox Header="Dynamic changes" Height="77" HorizontalAlignment="Left" Margin="5,98,0,0" Name="GbxNodes" VerticalAlignment="Top" Width="463" > 
<Grid> 
    <TextBlock Height="20" Margin="28,7,0,0" Name="TbDelta" Text="Delta X:" Width="48" />       
    <controls:WaterMarkNumericInput x:Name="TBDelta" Height="20" Width="119" IsNegativeSignAllowed="False" Margin="90,4,0,0" Style="{StaticResource goStRWMNI}" Number="{Binding Path=Delta, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" IsNullAllowed="False"/> 
    <TextBlock Height="20" Margin="223,6,0,0" Name="TbCounter" Text="XX' nodes counter:" Width="109" /> 
    <controls:WaterMarkNumericInput x:Name="TBCounter" IsEnabled="False" Height="20" Width="111" Margin="330,4,0,0" Style="{StaticResource goStRWMNI}" Number="{Binding Path=Counter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True,NotifyOnValidationError=True}" IsNullAllowed="False" /> 
</Grid> 
</GroupBox> 

我已經加入綁定和性能變化,在開始的時候Counter = 100,但我不知道如何更新CounterDelta更改並反之亦然

這裏是c#代碼

public partial class myClass : UserControl 
{ 
    double gnXmin, gnXmax, gnCounter; 
    int gnDelta; 
    public event PropertyChangedEventHandler PropertyChanged; 
    public double Xmin 
    { 
     get { return gnXmin; } 
     set { gnXmin = value; OnPropertyChanged("Xmin"); } 
    } 
    public double Xmax 
    { 
     get { return gnXmax; } 
     set { gnXmax = value; OnPropertyChanged("Xmax"); } 
    } 
    public int Delta 
    { 
     get { return gnDelta; } 
     set { gnDelta = value; OnPropertyChanged("Delta"); } 
    } 
    public double Counter 
    { 
     get { return gnCounter; } 
     set { gnCounter = value; OnPropertyChanged("Counter"); } 
    } 
    public myClass(double _Xmin, double _Xmax) 
    { 
     this.Xmin = _Xmin; 
     this.Xmax = _Xmax; 
     this.Counter = 100; 
     this.Delta = (int)((this.Xmax - this.Xmin)/(this.Counter - 1)); 
    } 
    void OnPropertyChanged(string prop) 
    { 
     if (this.PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
    } 

} 

我怎樣才能更新兩個要素或結合他們,讓其他的時候被修改,但考慮XminXmax性質每一個被修改?

我試圖把更新代碼中的getter和setter,但作爲一個字段取決於XminXmax我得到一個無限循環的例外......

回答

1

爲了避免無限循環,在做任何事情之前請確保通過value真的是從二傳手的當前值不同,:

public double Counter 
{ 
    get { ... } 
    set 
    { 
     if (gnCounter != value) 
     { 
      gnCounter = value; 
      Delta = (int)((Xmax - Xmin)/(gnCounter - 1)); 
      OnPropertyChanged("Counter"); 
     } 
    } 
} 

當然,您必須對Delta屬性也做同樣的事情。

1

回答這個問題「如何更新Counter在三角洲改變,反之亦然? ?」 Delta值更改時通知計數器,計數器更改時通知Delta。如果此解決方案犯規幫助,然後讓我知道

public int Delta 
    { 
     get { return gnDelta; } 
     set { gnDelta = value; 
       OnPropertyChanged("Delta"); 
       OnPropertyChanged("Counter");} 
    } 
    public double Counter 
    { 
     get { return gnCounter; } 
     set { gnCounter = value; 
      OnPropertyChanged("Counter"); 
      OnPropertyChanged("Delta"); } 
    }