我有2個文本字段代表Xmin
和Xmax
四邊形的值,此值初始化的構造函數爲myClass
。更新2個字段取決於每個其他值,但每個也取決於其他屬性值
我也有2個領域,Delta
和Counter
,這取決於Xmin
和Xmax
他們必須使用公式更新:
this.Delta = (int)((this.Xmax - this.Xmin)/(this.Counter - 1));
和
this.Counter = ((this.Xmax - this.Xmin)/(this.Delta)) + 1 ;
這似乎不是什麼難事,但是我想允許Delta
和Counter
更新,如果其他更改,即如果Delta
更改然後Counter
更新ES,反之亦然,這一切 考慮Xmin
和Xmax
值
這裏是我有
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
,但我不知道如何更新Counter
當Delta
更改並反之亦然
這裏是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));
}
}
我怎樣才能更新兩個要素或結合他們,讓其他的時候被修改,但考慮Xmin
和Xmax
性質每一個被修改?
我試圖把更新代碼中的getter和setter,但作爲一個字段取決於Xmin
和Xmax
我得到一個無限循環的例外......