【问题标题】:WPF Binding ProblemWPF 绑定问题
【发布时间】:2010-12-09 21:06:24
【问题描述】:

我有这个对象:

    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 显然具有正确的属性,那么为什么这不起作用?

【问题讨论】:

    标签: c# wpf binding


    【解决方案1】:

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

    【讨论】:

    • 他在问题中指出,他在代码隐藏中将集合分配给 ItemsSource。
    • 这是通过代码而不是 XAML 设置的。应该没问题吧?
    • 应该没问题,但尝试将其移至xaml。正如马特所提到的,我错过了问题中的那一行。也许您还需要将课程标记为公开...
    【解决方案2】:

    好的,你使用字段,但你需要属性

    class a 
    { 
        public string Application
        {
           get;set;
        }
        public DateTime From
        {
           get;set;
        } 
        public DateTime To
        {
           get;set;
        } 
    
    }
    

    【讨论】:

      【解决方案3】:

      看起来 WPF 不能直接绑定到字段,你必须使用这样的属性:

      class a
      {
          public string Application { get; set; }
          public DateTime From { get; set; }
          public DateTime To { get; set; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多