c# - 在 WinRT 中使用 MVVM Light Toolkit 填充文本框的进度条

标签 c# wpf xaml mvvm windows-runtime

我有一个包含几个 TextBox 元素和一个进度条的表单。我希望在 TextBox 分配了一些值时更新进度条。

因此,当一个值设置为 TextBox 时,如果长度不为 0,我会递增 ProgressPercent;

问题是我不知道使用什么条件来检查之前是否设置了任何值以及在 TextBox 再次变为空白时递减。

到目前为止你有我的代码

View 模型

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;
                }
            }
        }
    }

关于c# - 在 WinRT 中使用 MVVM Light Toolkit 填充文本框的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30784508/

相关文章:

c# - 如何在 Windows 应用商店应用程序中显示大量文本?

xaml - 将新的 Windows 8.1 搜索框设置为仅使用字形

c# - c++/c# 系统和程序资源监控 - Windows

wpf - F# WPF 事件 : "Block following this ' let' is unfinished. 需要一个表达式”

带有自定义参数的 C# 事件

c# - WPF 如何单击 DataGrid 中的行以取消选择项目

c# - 拖放选定的项目作为列表框返回null

c# - Xamarin.Forms.CarouselView 按钮的下一个和上一个项目事件

c# - NEST 2.0和ElasticSearch 2中的重大更改

c# - 如何在 Windows Phone 上调用 Setter 中的异步方法