【问题标题】:No binding error but still binding does not show anything?没有绑定错误但仍然绑定不显示任何内容?
【发布时间】:2011-01-05 21:54:58
【问题描述】:

我必须在我的 xaml/代码中进行哪些更改才能使与properties => SchoolclassName and LessonName 的绑定同时在TextBlocks 上工作?我没有收到任何绑定错误,但没有看到任何显示:

<Grid Margin="20" Height="300" Background="AliceBlue">
    <ListView ItemsSource="{Binding Timetable}">
        <ListView.View>
            <GridView>                    
                <GridView.Columns>                             
                    <GridViewColumn Header="Period"
                                    DisplayMemberBinding="{Binding LessonPeriod}"/>
                    <GridViewColumn Header="Monday">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ListView ItemsSource="{Binding Monday}">
                                    <ListView.ItemTemplate>
                                        <DataTemplate>
                                            <StackPanel Width="150" Orientation="Horizontal">
                                                <TextBlock Text="{Binding LessonName}"/>
                                                <TextBlock Text=" "/>
                                                <TextBlock Text="{Binding SchoolclassName}"/>
                                            </StackPanel>
                                        </DataTemplate>
                                    </ListView.ItemTemplate>
                                </ListView>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>  
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>


public partial class Window1 : Window
{
    List<TimetableEntry> _timetable = new List<TimetableEntry>();

    public List<TimetableEntry> Timetable
    {
        get { return _timetable; }
        set { _timetable = value; }   


    public Window1()
    {
        InitializeComponent();

        _timetable.Add(new TimetableEntry()

                            {
                                LessonPeriod = "Period 1",
                                Monday = new TimetableDay()
                                {
                                    LessonName = "Maths" ,
                                    SchoolclassName = "1c",                                        
                                },

                            }
                       );

        this.DataContext = this;
    }


    public class TimetableEntry
    {
        public string LessonPeriod { get; set; }
        public TimetableDay Monday { get; set; }
        public TimetableDay Tuesday { get; set; }
        public TimetableDay Wednesday { get; set; }
        public TimetableDay Thursday { get; set; }
        public TimetableDay Friday { get; set; }
        public TimetableDay Saturday { get; set; }
        public TimetableDay Sunday { get; set; }
    }

    public class TimetableDay
    {
        public string LessonName { get; set; }
        public string SchoolclassName { get; set; }
    }

    public class TimetableLesson
    {
        public string LessonName { get; set; }
        public string SchoolclassName { get; set; }
        public DateTime LessonTime { get; set; }
    }
}

【问题讨论】:

    标签: wpf data-binding listview binding properties


    【解决方案1】:

    您将内部 ListView 的 ItemsSource 设置为 Monday 属性。但是 Monday 是 TimetableDay,而不是集合,因此内部列表视图没有“列表”可以枚举。

    您可能只是想摆脱内部 ListView,并使用 Monday 属性的子属性直接将 DataTemplate 绑定到 TimetableEntry:

    <GridViewColumn.CellTemplate>
      <DataTemplate>
        <StackPanel Width="150" Orientation="Horizontal">
          <TextBlock Text="{Binding Monday.LessonName}" />  <!-- note multipart path -->
          <TextBlock Text=" " />
          <TextBlock Text="{Binding Monday.SchoolclassName}" />
        </StackPanel>
      </DataTemplate>
    </GridViewColumn.CellTemplate>
    

    【讨论】:

    • 是的,你是对的,当我摆脱内部 ListView 时,它就可以工作了!但是我需要那个 ListView,因为我想添加另外 6 个 ListView.ItemTemplates 来为每个工作日添加 6 个列:星期一、星期二、星期三...如果不再次引入 List,我该如何处理?
    • ups 对不起我的失败我忘了添加星期二等 GridViewColumn ,现在可以使用了!
    • 啊,我的检查和测试真的太快了;P 现在的问题是,在我现在使用的 DataGrid 中,我现在使用的不是 ListView ,课程从星期二开始,星期一停止。 .. 看图说清楚:[URL=img64.imageshack.us/i/classn.png/][IMG]http://… 星期二的课程在星期一的课程之后直接开始。我怎样才能让课程在星期二的 1、2、3 小时开始?
    • 因为我只能发布 600 个字符,所以我将新代码放在了 pastebin 上:pastebin.com/m3bcdc927
    • 您需要重组您的类以反映视图的需求。 (你不需要扭曲你的模型来做到这一点;你可以通过添加一个“视图模型”适配器层来做到这一点。)在视图中,你希望每个课程期间有一行,在该行中包含周一、周二的条目等。因此,您需要将 ItemsSource 设为 List&lt;Period&gt;,其中每个 Period 将具有星期一、星期二等属性。例如。时段 1 的星期一 = 数学 12c,星期二 = BWL 4a,等等。您可以通过合并具有相同时段的条目,从现有的 List&lt;TimetableEntry&gt; 构建这个“视图模型”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-23
    • 2011-12-15
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多