Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have a custom control library named CustomControl1 and a generic.xaml file in the theme folder for its layout.I took a list view in generic.xaml and now i want to bind it with the dataset.I tried a lot but couldn't find the way to bind it.Please help me out.


This is my generic.xaml

<ResourceDictionary
    x:Class="ListViewCustomControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfCustomControlLibrary2">
    <Style TargetType="{x:Type local:ListViewCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:ListViewCustomControl}">                    
                    <Border removed="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">                       
                        <ListView x:Name="ListView1" ItemsSource="{Binding ObjData}" Width="600">                                                        
                           <ListView.View>
                                <GridView>                                    
                                    <GridViewColumn Header="Subject" DisplayMemberBinding="{Binding Subject}"
                                                Width="100"></GridViewColumn>
                                    <GridViewColumn Header="Due Date" DisplayMemberBinding="{Binding DueDate}"
                                                Width="100"></GridViewColumn>
                                    <GridViewColumn Header="Status" DisplayMemberBinding="{Binding Status}"
                                                Width="100"></GridViewColumn>
                                    <GridViewColumn Header="Priority" DisplayMemberBinding="{Binding Priority}"
                                                Width="100"></GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

This is my class function which returns dataset


C#
public class ListViewCustomControl : ListView
    {      
        static List<Record>  ObjData = new List<Record>();

        static ListViewCustomControl()
        {           
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ListViewCustomControl), new FrameworkPropertyMetadata(typeof(ListViewCustomControl)));                      
        }

        public void GetData()
        {
            Sp_Activity_Task_BindGridTableAdapter SqlAdptr = new Sp_Activity_Task_BindGridTableAdapter();
            foreach (DataRow Row in SqlAdptr.GetMeetings())
            {               
                ObjData.Add(new Record(Row["Subject"].ToString(), Row["DueDate"].ToString(),
                    Row["Status"].ToString(), Row["Priority"].ToString()));
            }
        }

        public ListViewCustomControl()
        {           
            GetData();
            DataContext = ObjData;
        }
    }

    public class Record
    {       
        private  string _Subject;
        private  string _DueDate;
        private  string _Status;
        private  string _Priority;
       
        public string Subject
        {
            get
            {
                return _Subject;
            }
            set
            {
                _Subject = value;
            }
        }
        public string DueDate
        {
            get
            {
                return _DueDate;
            }
            set
            {
                _DueDate = value;
            }
        }
        public string Status
        {
            get
            {
                return _Status;
            }
            set
            {
                _Status = value;
            }
        }
        public string Priority
        {
            get
            {
                return _Priority;
            }
            set
            {
                _Priority = value;
            }
        }

        public Record(string Subject,string DueDate,string Status,string Priority)
        {
            this._Subject = Subject;
            this._DueDate = DueDate;
            this._Status = Status;
            this._Priority = Priority;           
        }
    }

This is my window.xaml

C#
public partial class MainWindow : Window
{        
    public MainWindow()
    {
        InitializeComponent();         
    }
   
}


How will i bind the returned dataset with my listview in generic.xaml?
Posted
Updated 28-Jan-14 23:03pm
v4
Comments
Sumit Bhargav 25-Jan-14 1:02am    
Is there no one who can help me out?
Sergey Alexandrovich Kryukov 25-Jan-14 2:47am    
Hardly... It depends on what are you binding with, what events do you want the binding to trigger...
—SA
Sumit Bhargav 29-Jan-14 5:04am    
I updated the question recently ,is this could help you in understand the problem
Sergey Alexandrovich Kryukov 29-Jan-14 10:54am    
So, what's the problem?
—SA
Baxter P 1-Feb-14 22:11pm    
I think you need to create a ListViewCustomControl, and then set it's DataContext to a class called ObjData which is of type Record. Also the Record class needs to be a View Model, not a regular class, or it wont update the view.

I don't think you can do what you are suggesting, the data binding should be done when the ListViewCustomControl is used in a window or user control. In it's AXML file, or code behind file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900