gpt4 book ai didi

c# - WPF双向数据绑定(bind)列表框在WPF中建模无法获取选定值

转载 作者:太空宇宙 更新时间:2023-11-03 23:27:58 25 4
gpt4 key购买 nike

我正在为 WPF 中的数据绑定(bind)而苦苦挣扎,在四处搜索之后,我仍然看不到我缺少什么来让模型更新列表中的选定项目。我有 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>

为了测试,我放入了一个绑定(bind)到 slotstartdatetime 的文本框,它可以工作并更新回我的模型。我在列表框上尝试了多种绑定(bind)格式,并且可以获得要显示的 SlotTypes 列表,但无法使 Slot 实体更新为所选值。

我意识到这已经成为一个很长的问题,但如果有人能看出我做错了什么,请问一下?

最佳答案

代码有几个问题:

  1. visual studio OUTPUT 窗口中有两个绑定(bind)错误(在定义列表框源并包含元素的情况下),原因是 SelectedValuePath 的错误绑定(bind)和源类型冲突。以下是适合我的解决方案。
  2. 数据上下文收集初始化:

    private void InitDataContext()
    {
    DataContext = new SlotWithTypes
    {
    slot = new Slot(),
    slotTypes = new ObservableCollection<SlotType>(new List<SlotType>
    {
    new SlotType
    {
    SlotTypeDesc = "Bla Bla 1",
    SlotTypeID = 1,
    },
    new SlotType
    {
    SlotTypeDesc = "Bla Bla 2",
    SlotTypeID = 2,
    }
    })
    };
    }
  3. Xaml 代码:

    <Grid>
    <ListBox Name="lstSlotTypes" HorizontalAlignment="Left"
    Height="200" Margin="0,10,0,0"
    VerticalAlignment="Top" Width="194"
    ItemsSource="{Binding slotTypes}"
    SelectedValue="{Binding SelectedSlotType, Mode=TwoWay}"
    SelectedValuePath="SlotTypeID" DisplayMemberPath="SlotTypeDesc"
    SelectionChanged="lstSlotTypes_SelectionChanged">
    </ListBox></Grid>
  4. SlotWithTypes 代码:

    public ObservableCollection<SlotType> slotTypes { get; set; }

    public Slot slot
    {
    get { return _slot; }
    set
    {
    _slot = value;
    OnPropertyChanged();
    }
    }

    public int SelectedSlotType
    {
    get { return _selectedSlotType; }
    set
    {
    _selectedSlotType = value;
    OnPropertyChanged();
    UpdateSlot(SelectedSlotType);
    }
    }

    private void UpdateSlot(int selectedSlotType)
    {
    slot.SlotType = selectedSlotType;
    }
  5. 因此,每次我进行选择时,插槽的 SlotType 属性都会更改,并使用新值定义广告。在这里做你的逻辑。

问候,

关于c# - WPF双向数据绑定(bind)列表框在WPF中建模无法获取选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33232707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com