Reputation: 51
Im new on wpf, and ive been having this problem...
I want to show a "Complex" object in my view the complex object named WeeklySchedule:
that have a list of "Shifts"
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; }
.....
}
Im using two Data Templates to try and show the content in these objects:
<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>
In the 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);
}
}
When i run it i get this binding errors:
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')
I really dont understand that much of the error, is it trying to find the property SelectedShift inside the WeeklySchedule class??
i tried to make it as clear as possible... Any ideas?, Thanks in advance
Upvotes: 0
Views: 300
Reputation: 43636
Your DataTemplate
DataContext
is of type WorkScheduleViewModel
, and SelectedShift
does not exist in WorkScheduleViewModel
.
So you will have to set the ListViews
DataContext
to your ViewModel
Something like this should work
<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}"/>
Upvotes: 2