YWah
YWah

Reputation: 579

Update specific listview item value

I've a ListView like this:

<ListView.View>
  <GridView>
     <GridViewColumn x: Name = "KeyColumn1" Header = "Key" Width = "100" DisplayMemberBinding = "{Binding Path=Label}"/ >
     <GridViewColumn x: Name = "ValueColumn1" Header = "Value" Width = "130" DisplayMemberBinding = "{Binding Path=Value}"/>
  </GridView>
</ListView.View>

The time is defined like: public DateTime time = DateTime.Now;

How can I update the time in two different methods? I was able to get the time and display it in Ringing(), but the time is not update in Established().

private void Ringing()
{
  CallTabLv1.Items.Add(new { Label = "Time", Value = time.ToString() });
  CallTabLv1.Items.Add(new { Label = "Call Type", Value = "Call in" });
}

private void Established()
{
  CallTabLv1.Items.Refresh();
}

I know that the simplest way is clear the items and add again in Established(), but since there are more than two items need be added, I don't want the code looks lengthy and duplicated. The other way I've thought is remove the specific row and then insert again, but this method is not suitable since my data is dynamic.

Upvotes: 4

Views: 1533

Answers (2)

yo chauhan
yo chauhan

Reputation: 12295

Instead of using Anonymous Type create a type like

    public class LabelValuePair:INotifyPropertyChanged
{
    public bool  RequiresTimeRefresh{get { return !string.IsNullOrEmpty(Label) && Label.ToLower() == "time"; }}

    private string label;

    public string Label
    {
        get { return label;}
        set { label = value; }
    }

    private string value;

    public string Value
    {
        get { return value; }
        set { this.value = value; Notify("Value");}
    }

    public LabelValuePair(string label, string value)
    {
        this.Label = label;
        this.Value = value;
    }

    private void Notify(string propName)
    {
        if(PropertyChanged!=null)
            PropertyChanged(this,new PropertyChangedEventArgs(propName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

Ringing method

        private void Ringing(DateTime time)
    {
        CallTabLv1.Items.Add(new LabelValuePair("Time", time.ToString()));
        CallTabLv1.Items.Add(new LabelValuePair("Call Type", "Call in"));
    }

Established method

        private void Established()
    {
        foreach (LabelValuePair item in CallTabLv1.Items)
        {
            if (item.RequiresTimeRefresh)
                item.Value = DateTime.Now.ToString();
        }
    }

Now you wont even have to call Refresh.NotifyPropertyChanged will do that.

Upvotes: 1

user2080797
user2080797

Reputation:

The simplest way, is to build an array / list with the columns. Then cycle thru the list and find your 'key'. Whether this key is the primary key or a key of your own design is up to you. I would advise using your own key and creating a custom class, or you will create a mess later when all the lists have their own (or multiple) p-keys.

class Columns : MSColumnClass
{
    String        Name;   // name of db column
    CCData        value;  // CCData contains int / long / String / Date
}
class row_data
{
    std::list<Columns>      *m_colum
    long                    row;
    long                    my_key;
}

Upvotes: 0

Related Questions