I'm struggling with databinding in WPF having searched around I still can't see what I am missing to get the model to update with the selected item from the list. I have 2 entities a Slot and a SlotType
public class Slot : BindableObject, INotifyPropertyChanged
{
private int slotID;
public int SlotID
{
get { return slotID; }
set
{
if (slotID != value)
{
slotID = value;
RaisePropertyChanged("SlotID");
}
}
}
private string user;
public string SlotUser
{
get { return user; }
set
{
if (user != value)
{
user = value;
RaisePropertyChanged("SlotUser");
}
}
}
private int slotUserID;
public int SlotUserID
{
get { return slotUserID; }
set
{
if (slotUserID != value)
{
slotUserID = value;
RaisePropertyChanged("SlotUserID");
}
}
}
private int slotType;
public int SlotType
{
get { return slotType; }
set
{
if (slotType != value)
{
slotType = value;
RaisePropertyChanged("SlotType");
}
}
}
private DateTime slotStartDateTime;
public DateTime SlotStartDateTime
{
get { return slotStartDateTime; }
set
{
if (slotStartDateTime != value)
{
slotStartDateTime = value;
RaisePropertyChanged("SlotStartDateTime");
}
}
}
public class SlotType : BindableObject, INotifyPropertyChanged
{
private int slotTypeID;
public int SlotTypeID
{
get { return slotTypeID; }
set
{
if (slotTypeID != value)
{
slotTypeID = value;
RaisePropertyChanged("SlotTypeID");
}
}
}
private string slotTypeDesc;
public string SlotTypeDesc
{
get { return slotTypeDesc; }
set
{
if (slotTypeDesc != value)
{
slotTypeDesc = value;
RaisePropertyChanged("SlotTypeDesc");
}
}
}
public override string ToString()
{
return SlotTypeDesc;
}
}
I then create a class containing both and set my window context to be this class.
public class SlotWithTypes
{
public ObservableCollection<SlotType> slotTypes { get; set; }
public Slot slot { get; set; }
}
From my main window I test and if required open new window displaying a list of SlotTypes for the user to select which type is relevant to the slot being created.
Slot slot = new Slot();
slot.SlotStartDateTime = item.SlotStartDateTime;
if (item.SlotType == 0)
{
SlotWithTypes swtype = new SlotWithTypes();
swtype.slotTypes = slotTypes;
swtype.slot = slot;
SelectSlotType stype = new SelectSlotType();
stype.DataContext = swtype;
stype.ShowDialog();
}
Finally in my XAML I have my listbox
<Grid>
<ListBox Name="lstSlotTypes"
HorizontalAlignment="Left"
Height="200"
Margin="0,10,0,0"
VerticalAlignment="Top"
Width="194"
ItemsSource="{Binding slotTypes}"
SelectedItem="{Binding Path=slot.Type, Mode=TwoWay}"
SelectedValue="{Binding slotTypes.SlotTypeID, Mode=TwoWay}"
SelectedValuePath="{Binding slot.Type, Mode=TwoWay}"
DisplayMemberPath="{Binding slotTypes.SlotTypeDesc}"
SelectionChanged="lstSlotTypes_SelectionChanged">
</ListBox>
<TextBox HorizontalAlignment="Left"
Height="23"
Margin="10,246,0,0"
TextWrapping="Wrap"
Text="{Binding slot.SlotStartDateTime, Mode=TwoWay}"
VerticalAlignment="Top"
Width="74"/>
</Grid>
For testing I put in a textbox bound to the slotstartdatetime which works and updates back to my model. I have tried a variety of binding formats on the listbox and can get my list of SlotTypes to display put can't get the Slot entity to update with the selected value.
I realise this has become a long question but please if someone can see what I'm doing wrong?