【发布时间】:2016-01-18 21:06:54
【问题描述】:
我在 WPF 中的数据绑定中苦苦挣扎,四处搜索后我仍然看不到我缺少什么来让模型使用列表中的选定项目进行更新。 我有 2 个实体,一个 Slot 和一个 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;
}
}
然后我创建一个包含两者的类并将我的窗口上下文设置为此类。
public class SlotWithTypes
{
public ObservableCollection<SlotType> slotTypes { get; set; }
public Slot slot { get; set; }
}
在我的主窗口中进行测试,如果需要,打开新窗口,显示 SlotTypes 列表,供用户选择与正在创建的插槽相关的类型。
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();
}
终于在我的 XAML 中我有了我的列表框
<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>
为了测试,我放入了一个绑定到 slotstartdatetime 的文本框,它可以工作并更新回我的模型。我在列表框上尝试了多种绑定格式,可以让我的 SlotTypes 列表显示 put can't get the Slot entity to update with selected value.
我意识到这已成为一个很长的问题,但如果有人能看出我做错了什么,请教一下?
【问题讨论】:
-
这是什么逻辑? 1. 打开窗口有物品清单吗? 2. 列表框中是否有任何项目?
-
您确定,在 SelectedValuePath 和 DisplayMemberPath 中您需要绑定吗? :) 必须有一些属性名称。
标签: c# wpf xaml data-binding listbox