开发者

Getting Objects In ListBoxView To Show Different Strings?

开发者 https://www.devze.com 2023-04-01 13:54 出处:网络
If I fill a ListBoxView with objects, the text that will display for each one will be the text produced by the ToString() function.Let\'s say I want to use ToString() in some cases, and GetSpecialStri

If I fill a ListBoxView with objects, the text that will display for each one will be the text produced by the ToString() function. Let's say I want to use ToString() in some cases, and GetSpecialString() in other cases. What is the best way to switch between the two?

Should I have two different objects with the same base class with different ToString() methods, or is there a 开发者_开发问答way I can have both the ToString() and GetSpecialString() methods in the same class?


You could make a class-wrapper for your view model, which returns different strings by condition:

class ViewModelListItem {

  public ViewModelListItem(MyObject item) {
    this.Item = item;
  }

  public MyObject Item {
    get;
    private set;
  }

  public override ToString() {
    // to do: add your logic here
    if (...)
      return "case A";
    else
      return "Case B";
  }
}

Then just fill your ListView with such items.


If the choice between ToString() and GetSpecialString() is esclusive, just override ToString() in class you want and done.

0

精彩评论

暂无评论...
验证码 换一张
取 消