当前位置:  开发笔记 > 编程语言 > 正文

ListView绑定与ListViewItem的"IsSelected"属性

如何解决《ListView绑定与ListViewItem的"IsSelected"属性》经验,为你挑选了1个好方法。

我有下课

   public abstract class AbsTrinityEvent
   {
    public event IsSelected OnSelectedEvent; 

    bool _IsSelected;
    ITrinityEvent _objTrinityEvent;
    public AbsTrinityEvent(ITrinityEvent objTrinityEvent)
    {
        _objTrinityEvent = objTrinityEvent;
    }


    public ITrinityEvent TrinityEventObj
    {

        set
        {
            _objTrinityEvent = value;
        }

        get
        {
            return _objTrinityEvent;
        }
    }

    public int EventRefID
    {
        get
        {
            return _objTrinityEvent.EventRefID;
        }
    }

    public string EventDescription
    {
        get
        {
            return _objTrinityEvent.EventDescription;
        }
    }

    public string EventDateTime
    {
        get
        {
            return _objTrinityEvent.EventDateTime;
        }
    }

    public string Site
    {
        get
        {
            return _objTrinityEvent.Site;
        }
    }

    public int Priority
    {
        get
        {
            return _objTrinityEvent.Priority;
        }
    }

    public string DeviceName
    {
        get
        {
            return _objTrinityEvent.DeviceName;
        }
    }

    public bool IsAlarm
    {
        get
        {
            return _objTrinityEvent.IsAlarm;
        }
    }

    public string OperatorName
    {
        get
        {
            return _objTrinityEvent.OperatorName;
        }
    }

    public int SiteID
    {
        get
        {
            return _objTrinityEvent.SiteID;
        }
    }

    public int EventSrcInstanceID
    {
        get
        {
            return _objTrinityEvent.EventSrcInstanceID;
        }
    }


    public int EventSrcInstanceMasterDeviceID
    {
        get
        {
            return _objTrinityEvent.EventSrcInstanceMasterDeviceID;
        }
    }

    public bool IsSelected
    {
        set
        {
            _IsSelected = value;
            ItemSelectedEventArgs obj = new ItemSelectedEventArgs(_objTrinityEvent);
            OnSelectedEvent(this, obj);                                        
        }
        get
        {
            return _IsSelected;
        }
    }      

}

public class ItemSelectedEventArgs : EventArgs
{
    private ITrinityEvent _objItem;

    public ItemSelectedEventArgs(ITrinityEvent objItem)
    {
        _objItem = objItem;

    }

    public ITrinityEvent SlectedNode
    {
        get
        {
            return _objItem;
        }
    }
}

public sealed class TrinityEventData : AbsTrinityEvent
{
    public TrinityEventData(ITrinityEvent objEvent)
        : base(objEvent)
    {
    }
}

我使用以下函数将此绑定到我的listview中的代码后面(不在XAML中)

    public void SetupColumnsForUnAcklist()
    {
        //Create Columns for listview
        GridView grdView = new GridView();
        grdView.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding() { Path = new PropertyPath("EventDescription") }, Header = "Description" });
        grdView.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding() { Path = new PropertyPath("EventDateTime") }, Header = "Date:Time" });
        grdView.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding() { Path = new PropertyPath("Site") }, Header = "Site" });
        grdView.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding() { Path = new PropertyPath("DeviceName") }, Header = "Device" });
        grdView.Columns.Add(new GridViewColumn() { DisplayMemberBinding = new Binding() { Path = new PropertyPath("Priority") }, Header = "Priority" });



        lstview_Unack.View = grdView;                   

        //Do Binding 
        if (_alarmUnAckList != null)
        {

            lstview_Unack.SetBinding(ListView.ItemsSourceProperty, new Binding() { Source = _alarmUnAckList });
            lstview_Unack.SetBinding(ListView.IsSelectedProperty, new Binding() { Path = new PropertyPath("IsSelected") });
        }

        lstview_Unack.ContextMenu = contextMenu;

        foreach (GridViewColumn col in grdView.Columns)
        {
            comboColumnList.Items.Add(col.Header as string);
        }           
    }     

我的问题是,我想将ListViewItem"IsSelected"属性绑定到TrinityEventData的"IsSelected"属性.我应该怎么做后面的代码.

请帮忙.

关心Sandeep



1> Robert Macne..:

首先,你在XAML中做得更好.它使事情更清晰,更短.我将在XAML和代码隐藏中回答这个问题.

最简单的方法是将Style应用于ListViewItem并使用Setter应用绑定.在ListViewItem上,DataContext将成为您的绑定项(在本例中为TrinityEventData).

假设你在XAML中有你的ListView:


    
        
    

在代码中,您必须手动构造Style,Setter和Binding:

Style listViewItemStyle = new Style { TargetType = typeof(ListViewItem) };
listViewItemStyle.Setters.Add(new Setter
{
    Property = ListViewItem.IsSelectedProperty,
    Value = new Binding { Path = new PropertyPath("IsSelected") }
});
lstview_Unack.Resources.Add(typeof(ListViewItem), listViewItemStyle);

但是,这和虚拟化存在问题.如果您的ListViewItems被虚拟化,您可能会取消选择ListView中的项目,但绑定将不会被触发,因为您的ListViewItem将不存在.

推荐阅读
家具销售_903
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有