views:

2132

answers:

2

Hi All, I have following class

   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)
    {
    }
}

I am binding this to my listview in code behind ( Not in XAML ) using following function

    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);
        }           
    }

My problem is, I want bind ListViewItem "IsSelected" Property to the TrinityEventData's "IsSelected" Property. How I should I do it in code behind.

Please help.

Regards Sandeep

+2  A: 

First off, you're much better off doing this in XAML. It makes things much clearer and shorter. I'm going to answer in both XAML and code-behind to demonstrate this.

The easiest way is to make a Style applied to ListViewItem and using a Setter to apply the binding. On a ListViewItem, the DataContext is going to be your bound item (TrinityEventData in this case).

Assuming you had your ListView in XAML:

<ListView x:Name="lstview_Unack">
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
        </Style>
    </ListView.Resources>
</ListView>

In code, you have to construct the Style, Setter, and Binding by hand:

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);

There are issues with this and virtualization, however. If your ListViewItems get virtualized, you might be unselecting items in the ListView but the binding won't be firing because your ListViewItem won't exist.

Robert Macnee
Thanks for this thorough answer. I was having a lot of trouble figuring out how to do this!
emddudley
A: 

Here:

http://simplesample.site90.com/wpf_binded_listview.php

you'll find a downlodable sample showing how to retrieve the SelectedItem of a ListView (and manipulate it) instead of manipulating any property inside the Item itself.

Rafa