代码之家  ›  专栏  ›  技术社区  ›  Blindy

WPF绑定问题

  •  3
  • Blindy  · 技术社区  · 16 年前

    我有一个目标:

        class a 
        { 
            public string Application; 
            public DateTime From, To;
        }
    

    我在此声明:

        ObservableCollection<a> ApplicationsCollection = 
            new ObservableCollection<a>();
    

    在我的XAML中,我有:

        <ListView Height="226.381" Name="lstStatus" Width="248.383" HorizontalAlignment="Left" Margin="12,0,0,12" VerticalAlignment=">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="140" Header="Application"
                                    DisplayMemberBinding="{Binding Path=Application}"/>
                    <GridViewColumn Width="50" Header="From" 
                                    DisplayMemberBinding="{Binding Path=From}"/>
                    <GridViewColumn Width="50" Header="To" 
                                    DisplayMemberBinding="{Binding Path=To}"/>
                </GridView>
            </ListView.View>
        </ListView>
    

    当我这样做时:

            lstStatus.ItemsSource = ApplicationsCollection;
    

    我收到一堆错误,但在我的列表视图中没有显示任何内容:

    System.Windows.Data Error: 39 : BindingExpression path error: 'Application' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=Application; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
    System.Windows.Data Error: 39 : BindingExpression path error: 'From' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=From; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
    System.Windows.Data Error: 39 : BindingExpression path error: 'To' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=To; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
    

    a a的属性显然是正确的,为什么这不起作用呢?

    3 回复  |  直到 16 年前
        1
  •  7
  •   Matthias Schippling    16 年前

    class a
    {
        public string Application { get; set; }
        public DateTime From { get; set; }
        public DateTime To { get; set; }
    }
    
        2
  •  3
  •   Ilya Khaprov    16 年前

    class a 
    { 
        public string Application
        {
           get;set;
        }
        public DateTime From
        {
           get;set;
        } 
        public DateTime To
        {
           get;set;
        } 
    
    }
    
        3
  •  -2
  •   Dani    16 年前

    检查这篇文章- http://www.codeproject.com/KB/miscctrl/GridView_WPF.aspx 我认为您缺少ItemsSource=指令。