0

我是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>

在视图模型中:

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 ?

我试图让它尽可能清楚......有什么想法吗?,在此先感谢

4

1 回答 1

2

DataTemplate DataContext的类型是WorkScheduleViewModel,并且SelectedShift不存在于WorkScheduleViewModel. 所以你必须ListViews DataContextViewModel

像这样的东西应该工作

   <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}"/>
于 2013-04-11T23:05:23.250 回答