简体   繁体   English

如果绑定值不是“”,如何修改绑定以使其添加“ pts”?

[英]How can I modify a binding so that it adds “ pts” if the bound value is not “”?

I am using a ViewModel to set the number of points on my display. 我正在使用ViewModel设置显示器上的点数。 Is there a way that I could modify this so that when the value of points is "0" or another not empty that it adds " pts" but when it's "" it still displays "" 有没有一种方法可以对此进行修改,以便当点的值为“ 0”或另一个不为空时,它添加“ pts”,但当其为“”时仍显示“”

    string _points;
    public string Points
    {
        get => _points;
        set => SetProperty(ref _points, value);
    }

Do a check if the _string is "0" before returning it to the view, in your Points property: _string返回到视图之前,在Points属性中检查_string是否为"0"

get => _points.Equals("0") ? "0 pts" : _points;

And to tidy it up, put it into a method: 并整理一下,将其放入方法中:

public string ReturnPoints() =>  _points.Equals("0") ? "0 pts" : _points;

And then in your Points property: 然后在您的Points属性中:

string _points;
public string Points
{
    get => ReturnPoints();
    set => SetProperty(ref _points, value);
}

From your question, you can try this. 根据您的问题,您可以尝试一下。

use string.IsNullOrEmpty method to check string whether empty, if no append pts to the end string, otherwise return "" . 使用string.IsNullOrEmpty方法检查字符串是否为空,如果没有将pts附加到末尾字符串,否则返回""

string _points;
public string Points
{  
    get => !string.IsNullOrEmpty(_points) ? $"{_points} pts" : string.Empty;
    set => SetProperty(ref _points, value);
}

Based on replys from OP on my other answer. 根据OP对我其他答案的答复。

public static Points _points { get; set; }

class Points
{
    private int? i; 

    public static implicit operator Points(int value)
    {
        return new Points { i.Value = value };
    }

    public static implicit operator string(Points value)
    {
         if(Value.i.HasValue)
             return value.i.Value.ToString();
          return "";
    }

     public static implicit operator int(Points value)
    {   
        return value.I.HasValue ?value.i.value : 0;
    }
}

and using the new Points struct: 并使用新的Points结构:

_points += 2;         // 2
string s = points;    // "2"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修改中继器以不显示绑定到它的每一行的数据? - How can I modify a repeater to not show data for every row bound to it? 我该如何修改列表中的值 <t> ? - how can i modify a value in list<t>? 如果listBox中的项目超出了右边的范围,那么它将如何向下移动一行呢? - How can i make that if an item in a listBox is out of the bound on the right so it will move one line down? 将ComboBox与Generic List绑定,以便组合框项目的值表示绑定对象 - Binding ComboBox with Generic List so that value of combobox item represents bound object 如何修改此C#代码以便Visual Studio认识到我不是白痴? - How can I modify this C# code so that Visual Studio recognizes that I'm not an idiot? 如何修改此方法以便我可以引用或传递每个循环的数据? - How would I modify this method so I can reference or pass the data of each individual loop? 如何更改IEnumerable集合,以便可以将其绑定到数据网格 - How to change an IEnumerable collection so that it can be bound to a datagrid 如何修改我的快速排序算法,以便可以对Double数据类型数组进行排序? - How do I modify my Quick Sort Algorithm so that it can sort Double data type arrays? 如何在将值显示在数据绑定中继器中之前对其进行修改? - How can I modify a value before it is shown in a databound Repeater? 如何修改此查询以省略空格或空值? - How can i modify this query to leave out blanks or empty value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM