DataTemplate中的DataTemplate

时间:2013-04-11 22:28:30

标签: c# wpf

我是wpf的新手,我一直有这个问题......

我想在我的视图中显示一个“复杂”对象 名为WeeklySchedule的复杂对象:

有一个“班次”列表

public class WeeklySchedule
{
    public virtual IEnumerable<Shift> Shifts { get; set; }
    .....
}


public class Shift
{
    public virtual String EntryTime { get; set; }
    public virtual String ExitTime { get; set; }
    .....
}

我使用两个数据模板尝试显示这些对象中的内容:

<DataTemplate x:Key="ShiftlistViewTemplate" DataType="viewModel:WorkScheduleViewModel">
            <TextBox Text="{Binding EntryTime}"/>
            <TextBox Text="{Binding ExitTime}"/>
        </DataTemplate>

        <DataTemplate x:Key="WeeklySchedulelistViewTemplate"
                      DataType="viewModel:WorkScheduleViewModel">
                    <ListView x:Name="ShiftListView" 
                              Grid.Column="0"
                              ItemTemplate="{StaticResource ShiftlistViewTemplate}" 
                              ItemsSource="{Binding Shifts}"
                              SelectedItem="{Binding SelectedShift, Mode=TwoWay}"/>
       </DataTemplate>

在viewModel中:

public class ViewModel : WorkspaceViewModel
{
     public Shift SelectedShift
    {
        get
        {
            return _selectedShift;
        }

        set
        {
            if (_selectedShift == value)
            {
                return;
            }

            _selectedShift = value;
            RaisePropertyChanged(SelectedShiftPropertyName);
        }
    }
     public ObservableCollection<WorkSchedule> WorkSchedules
     {
        get
        {
            return _workSchedules;
        }

        set
        {
            if (_workSchedules == value)
            {
                return;
            }

            _workSchedules = value;
            RaisePropertyChanged(WorkSchedulePropertyName);
        }
     }

    public ObservableCollection<Shift> Shifts
    {
        get
        {
            return _shifts;
        }

        set
        {
            if (_shifts == value)
            {
                return;
            }

            _shifts = value;
            RaisePropertyChanged(ShiftPropertyName);
        }
}

当我运行它时,我得到了这个绑定错误:

System.Windows.Data Error: 40 : BindingExpression  path error: 'SelectedShift' property 
not found on 'object' ''WeeklySchedule' (HashCode=7843366)'. 
BindingExpression:Path=SelectedShift; DataItem='WeeklySchedule' (HashCode=7843366);
 target element is 'ListView' (Name=''); target property is 'SelectedItem' (type 'Object')

我真的不明白那个错误,是不是试图在WeeklySchedule类中找到属性SelectedShift?

我试图让它尽可能清晰...... 有什么想法?,提前致谢

1 个答案:

答案 0 :(得分:2)

DataTemplate DataContext类型为WorkScheduleViewModelSelectedShift中不存在WorkScheduleViewModel。 因此,您必须将ListViews DataContext设置为ViewModel

这样的事情应该有效

   <ListView  x:Name="ShiftListView" 
              DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ViewModel}}}"
              Grid.Column="0"
              ItemTemplate="{StaticResource ShiftlistViewTemplate}" 
              ItemsSource="{Binding Shifts}"
              SelectedItem="{Binding SelectedShift, Mode=TwoWay}"/>