gpt4 book ai didi

c# - 网格下拉列表中的列表不返回值

转载 作者:行者123 更新时间:2023-12-03 10:55:57 25 4
gpt4 key购买 nike

我有一个枚举

enum SendDays
{
Maandag = 1,
Dinsdag,
Vandaag= 99
}
和一类
public struct DayListModel
{
public int Id;
public string DayName;
}
我用这样的日子填写 list
private void Filldays()
{
foreach(int i in Enum.getValues(typeof(SendDays)))
{
DayListModel day =new DaylistModel()
{
Id = i,
Dayname = Enum.GetName(typeof(SendDays), i)
};
DayList.Add(day);
}
当我使用它时,在radcombobox的A telerik RadGridView中
喜欢
<telerik:RadComboBox ItemsSource="{Bindning DayList}" DisplayMemberPath="DayName" SelectedValue="{Bindning DefaultSendDay}" SelectedValuePath="Id"/>
每当我更改selecteditem时,都不会通过。
有什么建议?
耶罗恩

最佳答案

这是我的建议,以及如何使它起作用:

  • 就像@ mm8一样,您应该将DayListModel设置为类而不是结构,然后将fields转换为properties。如果不使用属性,则DisplayMemberPath不起作用。有些东西不适用于结构(例如using == to compare),因此在这种情况下,我将选择一个类。
  • 您的对象在 View 模型中是否可观察?我不知道telerik,但是您应该调用某种PropertyChanged方法,以便可以更新UI。如果使用MVVM light之类的框架,则它具有ObservableObjectRaisePropertyChanged方法。

  • 这是一个工作示例: MainWindowVM.cs
    using GalaSoft.MvvmLight;
    using System;
    using System.Collections.Generic;

    namespace WpfApp1
    {
    public class MainWindowVM : ObservableObject
    {
    private List<DayListModel> _DayList;
    public List<DayListModel> DayList
    {
    get { return _DayList; }
    set
    {
    if (value != _DayList)
    {
    _DayList = value;
    RaisePropertyChanged();
    }
    }
    }


    private DayListModel _DefaultSendDay;
    public DayListModel DefaultSendDay
    {
    get { return _DefaultSendDay; }
    set
    {
    if (value != _DefaultSendDay)
    {
    _DefaultSendDay = value;
    RaisePropertyChanged();
    }
    }
    }

    public MainWindowVM()
    {
    DayList = new List<DayListModel>();
    foreach (int i in Enum.GetValues(typeof(SendDays)))
    {
    DayListModel day = new DayListModel()
    {
    Id = i,
    DayName = Enum.GetName(typeof(SendDays), i)
    };
    DayList.Add(day);
    }
    DayList = new List<DayListModel>(DayList);
    }
    }

    public enum SendDays
    {
    Maandag = 1,
    Dinsdag,
    Vandaag = 99
    }

    public class DayListModel
    {
    public int Id { get; set; }
    public string DayName { get; set; }
    }
    }


    MainWindow.xaml
    <Window x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
    <local:MainWindowVM />
    </Window.DataContext>

    <Grid >
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
    <ComboBox ItemsSource="{Binding DayList}" SelectedItem="{Binding DefaultSendDay}" DisplayMemberPath="DayName" >
    </ComboBox>
    <TextBlock Text="{Binding DefaultSendDay.DayName}"/>
    </StackPanel>
    </Grid>
    </Window>
    截屏:
    Screenshot of a combobox and textblock displaying the selectd value

    关于c# - 网格下拉列表中的列表不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64030144/

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