【问题标题】:WPF binding objects to Listbox, text not appearingWPF 将对象绑定到列表框,文本未出现
【发布时间】:2013-06-20 03:06:12
【问题描述】:

我搜索并找不到我的问题的答案,有人可以帮忙吗?我试图将对象列表绑定到列表框。我有正确数量的项目(我仍然可以选择它们),但我看不到文本。

我的 Xaml 代码:

<ListBox x:Name="listbox_leave" BorderThickness="0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding leaveName}" />
                <TextBlock Text="{Binding numberOfDays}" />
            </StackPanel>
       </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这是后端代码:

ObservableCollection<Leave> leavelist = new ObservableCollection<Leave>(); 
leavelist = DbControl.loadLeaveDetails();
listbox_leave.ItemsSource = leavelist;

还有我的假期班:

class Leave
{
    public string leaveName;

    public string LeaveName
    {
        get { return leaveName; }
        set { leaveName = value; }
    }

    public string numberOfDays;

    public string NumberOfDays
    {
        get { return numberOfDays; }
        set { numberOfDays = value; }
    }
}

当我调试时,Leave 的 ObservableCollection 列表包含所有正确的数据,我的列表框显示为空白,但其中包含正确数量的对象 Leave(我可以选择它们)但没有显示文本。我有这个消息:

System.Windows.Data Error: 40 : BindingExpression path error: 'leaveName' property not found on 'object' ''Leave' (HashCode=44930696)'. BindingExpression:Path=leaveName; DataItem='Leave' (HashCode=44930696); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 40 : BindingExpression path error: 'numberOfDays' property not found on 'object' ''Leave' (HashCode=44930696)'. BindingExpression:Path=numberOfDays; DataItem='Leave' (HashCode=44930696); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 40 : BindingExpression path error: 'leaveName' property not found on 'object' ''Leave' (HashCode=29274103)'. BindingExpression:Path=leaveName; DataItem='Leave' (HashCode=29274103); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

System.Windows.Data Error: 40 : BindingExpression path error: 'numberOfDays' property not found on 'object' ''Leave' (HashCode=29274103)'. BindingExpression:Path=numberOfDays; DataItem='Leave' (HashCode=29274103); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

有人可以帮忙吗?

【问题讨论】:

    标签: wpf binding


    【解决方案1】:

    您正在尝试绑定到字段(leaveNamenumberOfDays)而不是属性(LeaveNameNumberOfDays),这在 WPF 中不受支持。

    改成:

    <ListBox x:Name="listbox_leave" BorderThickness="0">
        <ListBox.ItemTemplate>
           <DataTemplate>
              <StackPanel>
                 <TextBlock Text="{Binding LeaveName}" />
                 <TextBlock Text="{Binding NumberOfDays}" />
              </StackPanel>
           </DataTemplate>
        </ListBox.ItemTemplate>
     </ListBox>
    

    另外,您可能应该继续填写privateleaveNamenumberOfDays)字段,而不是public

    【讨论】:

      猜你喜欢
      • 2014-11-05
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2011-09-20
      • 2012-05-09
      • 1970-01-01
      相关资源
      最近更新 更多