Rub*_*ben 2 wpf null properties mvvm
我实现了一个文本框,这绑定到一个viewmodel.我填写文本框默认为'100',但如果我将其更改为10,则属性始终设置正确.但是当我删除所有数字时,我希望该属性设置为null.但它只是在没有填充任何内容时设置值.他只保留最后一个值..
这是我的代码,viewmodel + xaml:
public double MaxTime
{
get
{
return maxTime;
}
set
{
maxTime = value;
OnPropertyChanged("MaxTime");
if (SelectedQuestionDropList != null)
{
foreach (QuestionCluster cluster in this.Examination.QuestionClusters)
{
if (cluster == SelectedQuestionDropList)
{
cluster.MaxTime = value;
}
}
}
}
}
<TextBox Height="23"
Visibility="{Binding Path=VisibleClusterDetails, Converter={StaticResource boolToVis},UpdateSourceTrigger=PropertyChanged}"
Text="{Binding Path=MaxTime,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"
HorizontalAlignment="Right" Margin="0,511,601,0" Name="textBox2"
VerticalAlignment="Top" Width="120" />
Run Code Online (Sandbox Code Playgroud)
这是一个约束性问题,你必须做三件事
1)将System命名空间导入xaml文件(注意sys关键字):
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)
2)然后在你的绑定中你需要告诉它当textbox中的值是string.Empty时发送null(这是文本框中没有任何内容的值,这就是为什么它不起作用,因为string.Empty可以' t转换为双).通过做这个:
<TextBox Text={Binding MaxTime,TargetNullValue={x:Static sys:String.Empty}}/>
Run Code Online (Sandbox Code Playgroud)
3)将ViewModel MaxTime属性更改为类型double?
| 归档时间: |
|
| 查看次数: |
3506 次 |
| 最近记录: |