Debugging 附加调试时,设置程序中包含逻辑的属性上的Silverlight 5绑定无法按预期工作
我的问题很容易重现。 我用视图模型从头开始创建了一个项目。 正如您在“Age”属性的setter中所看到的,我有一个简单的逻辑Debugging 附加调试时,设置程序中包含逻辑的属性上的Silverlight 5绑定无法按预期工作,debugging,binding,silverlight-5.0,Debugging,Binding,Silverlight 5.0,我的问题很容易重现。 我用视图模型从头开始创建了一个项目。 正如您在“Age”属性的setter中所看到的,我有一个简单的逻辑 public class MainViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private int age; public int Age {
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int age;
public int Age
{
get
{
return age;
}
set
{
/*Age has to be over 18* - a simple condition in the setter*/
age = value;
if(age <= 18)
age = 18;
OnPropertyChanged("Age");
}
}
public MainViewModel(int age)
{
this.Age = age;
}
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
如果文本框中输入的值小于18,我希望此代码将年龄限制为18岁。
场景:在文本框中插入值“5”,然后按tab键(要使绑定生效,文本框需要失去焦点)
案例1:已附加调试器=>
如预期,文本框值将为“5”,文本块值将为“18”。-错
案例2:未附加调试器=>
文本框值为“18”,文本块值为“18”-正确
当附加调试器时,绑定似乎无法在触发属性值更新的对象上按预期工作。只有当绑定到的属性在setter或getter中有一些逻辑时,才会发生这种情况
SL5中有什么变化吗?设置器中的逻辑不再被允许了
配置:
VisualStudio 2010 SP1
SL 5工具5.1.30214.0
SL5 sdk 5.0.61118.0
IE 10如果有人对答案感兴趣,请阅读以下内容:
<Grid x:Name="LayoutRoot" Background="White">
<TextBox
Text="{Binding Path=Age, Mode=TwoWay}"
HorizontalAlignment="Left"
Width="100"
Height="25"/>
<TextBlock
Text="{Binding Path=Age, Mode=OneWay}"
HorizontalAlignment="Right"
Width="100"
Height="25"/>
</Grid>
public partial class MainPage : UserControl
{
private MainViewModel mvm;
public MainPage()
{
InitializeComponent();
mvm = new MainViewModel(20);
this.DataContext = mvm;
}
}