代码之家  ›  专栏  ›  技术社区  ›  yoav.str

wpf-ui绑定如何?为什么这个示例代码不起作用

  •  0
  • yoav.str  · 技术社区  · 14 年前

    这是我的圣诞节主窗口:

    <Window x:Class="HR1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel HorizontalAlignment="Left" Width="162">
        <ListView ItemsSource="{Binding EventsCollection}" Background="AliceBlue" Height="311" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="event Date"  Width="112"/>
                </GridView>
            </ListView.View>
    
        </ListView>
        </StackPanel>
        <ScrollViewer>
            <StackPanel Margin="116,0,0,0" Width="284">
                <ListView Name="employeeListView"  ItemsSource="{Binding EmployeeCollection}">
                    <ListView.View>
                        <GridView>
    
                            <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}" Width="80"/>
                            <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}" Width="80"/>
                            <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}" Width="80"/>
                            <!--<GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}" Width="67" />
                            <GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}" Width="67"/>-->
    
    
    
                        </GridView>
    
                    </ListView.View>
    
                </ListView>
            </StackPanel>
        </ScrollViewer>
    </Grid>
    

    这里是代码背后的地方:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows; 
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using HR1.Model;
    
    namespace HR1
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    
        private CollectionView employeeCollection;
        private CollectionView eventsCollection;
    
        public MainWindow()
        {
            InitializeComponent();
            initlalizeEmployeeList();
            initlalizeEventDateList();
        }
    
        private void initlalizeEventDateList()
        {
        IList<employeeData> list = new List<employeeData>();
        //query to database should be here
        foreach (employeeData dataString in employeeDatesString)
        {
            list.Add(dataString );
        }
        employeeCollection = new CollectionView(list);
    }
    
    
    
        private void initlalizeEmployeeList()
        {
            IList<eventDate> list = new List<eventDate>();
            //query to database should be here
            foreach (eventDate dataString in eventDataString)
            {
                list.Add(dataString);
            }
            eventsCollection = new CollectionView(list);
        }
    
    
        #region collection binding setters 
        public CollectionView EmployeeCollection
        {
        get { return employeeCollection; }
        }
        public CollectionView EventsCollection
        {
            get { return eventsCollection; }
        }
        #endregion
    
        #region myDatabinding
        static employeeData []employeeDatesString = {new employeeData(1234,"yoav","stern "),new employeeData(1234,"yoav","stern ") };
        static eventDate[] eventDataString = { new eventDate("111"), new eventDate("2222") };
        #endregion
    
    
    }
    

    }

    这就是我存储在两个类中的数据,它们存储在两个地方:

     using System;
     using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace HR1.Model
    {
    class eventDate : INotifyPropertyChanged
    {
    
        public eventDate(string s) 
        {
            Date = s;
        } 
        #region private members
        private string date;
        #endregion
    
        #region proprties
        public string Date
        {
            get
            {
                return this.date;
            }
            set
            {
                date = value;
                if (value != this.date)
                {
                    this.date = value;
                    NotifyPropertyChanged("Date");
                }
            }
        }
        #endregion
    
        #region events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    
    }
    

    }

    第二个是:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace HR1.Model
    {
    class employeeData : INotifyPropertyChanged
    {
    public employeeData(long ID,string  f,string l){
        EmployeeID = ID;
        LastName = l;
        FirstName = f;
    }
    
    #region privete Members
    
    private long employeeID;
    private string firstName;
    private string lastName;
    
    #endregion
    
    #region proprties
    
    public long EmployeeID
    {
        get
    {
    return employeeID;
    }
    set
        {
            employeeID = value;
            if (value != this.employeeID)
            {
                this.employeeID = value;
                NotifyPropertyChanged("EmployeeID");
            }
         }
    }
    
    public string FirstName
    {
        get
        {
            return firstName;
        }
        set
        {
            firstName = value;
            if (value != this.firstName)
            {
                this.firstName = value;
                NotifyPropertyChanged("FirstName");
            }
        }
    }
    
    public string LastName
    {
        get
        {
            return lastName;
        }
        set
        {
            lastName = value;
            if (value != this.lastName)
            {
                this.lastName = value;
                NotifyPropertyChanged("LastName");
            }
        }
    }
    
    #endregion
    
    #region events
    public event PropertyChangedEventHandler PropertyChanged;
    #endregion
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
    }
    
    }
    

    注意:在我看来,在我宣布 员工协作 和xmal行:

    <ListView Name="employeeListView"  ItemsSource="{Binding EmployeeCollection}">  
    

    我做错了,因为这两个人没有像他们应该的那样结合在一起。

    3 回复  |  直到 14 年前
        1
  •  0
  •   Rob Fonseca-Ensor    14 年前

    如果你先换的话 <Grid> 到:

    <Grid DataContext="{Binding ElementName=window}">
    

    并添加 x:Name="window" 到根窗口元素,那么它能工作吗?

        2
  •  0
  •   ASanch    14 年前

    将主窗口构造函数更新为:

    public MainWindow()
            {
                InitializeComponent();
                initlalizeEmployeeList();
                initlalizeEventDateList();
    
                this.DataContext = this;
            }
    
        3
  •  0
  •   yoav.str    14 年前

    我发现了问题所在 我想做的事

    notifypropertychanged(“somelist”);

    例子:

       private void initlalizeEventDateList()
        {
        IList<employeeData> list = new List<employeeData>();
        //query to database should be here
        foreach (employeeData dataString in employeeDatesString)
        {
            list.Add(dataString );
        }
        employeeCollection = new CollectionView(list);
        NotifyPropertyChanged("employeeCollection");
    }