ListView绑定与ListViewItem的“IsSelected”属性

时间:2009-01-27 11:08:37

标签: wpf data-binding listview

我有以下课程

   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

2 个答案:

答案 0 :(得分:12)

首先,你在XAML中做得更好。它使事情更清晰,更短。我将在XAML和代码隐藏中回答来证明这一点。

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

假设您在XAML中有ListView:

<ListView x:Name="lstview_Unack">
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        </Style>
    </ListView.Resources>
</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将不存在。

答案 1 :(得分:0)

对我有用的是:

wc -l