用于在 WinRT 中使用 MVVM Light Toolkit 填充文本框的进度条
本文关键字:文本 填充 Toolkit Light WinRT MVVM 用于 | 更新日期: 2023-09-27 17:55:33
我有一个包含几个文本框元素和一个进度条的表单。我希望在文本框分配了一些值时更新进度条。
因此,当值设置为文本框时,如果长度不同于 0,我会递增进度百分比;
问题是我不知道使用什么条件来检查之前是否设置了任何值,并在文本框再次变为空白时递减。
贝娄到目前为止你有我的代码
视图模型
private string firstName { get; set; }
private string progressPercent { get; set; }
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
this.RaisePropertyChanged(() => this.FirstName);
var vm1 = (new ViewModelLocator()).MainViewModel;
if (value.Length != 0) // Checks the string length
{
vm1.ProgressPercent += 3;
}
}
}
public int ProgressPercent
{
get
{
return this.progressPercent;
}
set
{
this.progressPercent = value;
this.RaisePropertyChanged(() => this.ProgressPercent);
}
}
XAML
<StackPanel>
<ProgressBar x:Name="progressBar1"
Value="{Binding ProgressPercent ,Mode=TwoWay}"
HorizontalAlignment="Left"
IsIndeterminate="False"
Maximum="100"
Width="800"/>
<TextBlock Text="First Name"/>
<TextBox x:Name="FirstNameTextBox" Text="{Binding FirstName, Mode=TwoWay}"/>
</StackPanel>
任何想法如何做到这一点?
如果属性未更改,则不应通知属性更改。你总是可以确定它什么时候变空,反之亦然。
public string FirstName
{
get
{
return this.firstName;
}
set
{
if (this.firstName != value)
{
bool oldValueIsEmpty = String.IsNullOrWhiteSpace(this.firstName);
this.firstName = value;
this.RaisePropertyChanged(() => this.FirstName);
var vm1 = (new ViewModelLocator()).MainViewModel;
if (String.IsNullOrWhiteSpace(value)) // Checks the string length
{
vm1.ProgressPercent -= 3;
}
else if (oldValueIsEmpty)
{
vm1.ProgressPercent += 3;
}
}
}
}
像这样用布尔值跟踪:
private bool firstNamePoints=false;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
this.RaisePropertyChanged(() => this.FirstName);
var vm1 = (new ViewModelLocator()).MainViewModel;
if (value.Length != 0) // Checks the string length
{
if(!firstNamePoints)
{
vm1.ProgressPercent += 3;
firstNamePoints=true;
}
}
else
{
if(firstNamePoints)
{
vm1.ProgressPercent -= 3;
firstNamePoints=false;
}
}
}
}