我有2个文本字段,代表四边形的Xmin
和Xmax
值,这些值在myClass
的构造函数上初始化。
此外,我还有两个字段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
更新,反之亦然, 所有这些
考虑Xmin
和Xmax
值
这是我所拥有的
<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
我得到一个无限循环异常......
答案 0 :(得分:1)
回答这个问题&#34;如何在Delta改变时更新计数器,反之亦然?#34; 更改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"); }
}
答案 1 :(得分:1)
为了避免无限循环,在执行任何其他操作之前,请确保传递的value
确实与setter中的当前值不同:
public double Counter
{
get { ... }
set
{
if (gnCounter != value)
{
gnCounter = value;
Delta = (int)((Xmax - Xmin) / (gnCounter - 1));
OnPropertyChanged("Counter");
}
}
}
当然,您必须对Delta
属性执行相同操作。