【问题标题】:Progressbar for filling TextBox using MVVM Light Toolkit in WinRT在 WinRT 中使用 MVVM Light Toolkit 填充 TextBox 的进度条
【发布时间】:2015-08-27 08:49:59
【问题描述】:

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

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

问题是我不知道使用什么条件来检查之前是否设置了任何值,如果 TextBox 将再次变为空白,则减少。

到目前为止,你有我的代码

视图模型

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>

任何想法如何做到这一点?

【问题讨论】:

  • 如果它的长度为0,你不能只做else vm1.ProgressPercent-=3吗?或者这不是你要问的?
  • @bill 是的,但是你如何检查它之前是否有一个值,所以你可以减少?
  • 你可以使用布尔值来跟踪,所以当你为名字添加进度值时,设置firstNamePoints=true,如果长度为0,检查firstNamePoints=true,然后将其设置为false并减去3
  • var vm1 = (new ViewModelLocator()).MainViewModel; dafuq 你是不是用定位器找到当前实例来设置属性值?
  • @Will,我在不同的视图模型中设置属性

标签: c# wpf xaml mvvm windows-runtime


【解决方案1】:

如果属性未更改,您不应通知属性更改。你总是可以确定它什么时候变空,反之亦然。

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

【讨论】:

  • 好点,我更喜欢你的方法而不是 OP 当前的方法。
  • 我唯一的建议是使用IsNullOrWhiteSpace(如果在.Net 4+ 上编程)而不是IsNullOrEmpty,如果您不想只将空格注册为新值
  • 这种方法的问题是,如果你在第一次设置后编辑该值,ProgressPercent 会增加。你如何检查这是否只是一个编辑?
  • 你说得对,加了几项检查,不过属性太乱了,我还是放在单独的方法里吧。
【解决方案2】:

使用这样的 bool 进行跟踪:

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

【讨论】:

    猜你喜欢
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2011-06-03
    • 2011-02-10
    • 1970-01-01
    相关资源
    最近更新 更多