网格下拉框中的列表没有 return 值
List in dropdownbox in grid does not return value
我有一个枚举
enum SendDays
{
Maandag = 1,
Dinsdag,
Vandaag= 99
}
还有一个Class
public struct DayListModel
{
public int Id;
public string DayName;
}
我用这样的日子填表
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"/>
每当我更改所选项目时,这都不会通过。
有什么建议吗?
杰伦
确保类型匹配,即如果您的 DefaultSendDay
来源 属性 是 byte
,DayListModel
的 Id
也应该是byte
.
如果还是不行,尝试将字段转换为属性,将结构转换为 class:
public class DayListModel
{
public byte Id { get; set; }
public string DayName { get; set; }
}
这是我的建议,以及我是如何让它发挥作用的:
就像@mm8 说的,你应该把 DayListModel
做成 class 而不是结构,然后把 fields into properties. If you don't use properties, the DisplayMemberPath
doesn't work. Some stuff doesn't work with structs (like using ==
to compare) 变成 fields into properties. If you don't use properties, the DisplayMemberPath
doesn't work. Some stuff doesn't work with structs (like using ==
to compare) 所以我会用 class 在这种情况下。
你的对象在你的视图模型中是可观察的吗?我不知道 telerik,但您应该调用某种 PropertyChanged
方法,以便 UI 可以更新。如果你使用像 MVVM light 这样的框架,它有 ObservableObject
s 和 RaisePropertyChanged
方法。
这是一个工作示例:
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>
截图:
我有一个枚举
enum SendDays
{
Maandag = 1,
Dinsdag,
Vandaag= 99
}
还有一个Class
public struct DayListModel
{
public int Id;
public string DayName;
}
我用这样的日子填表
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"/>
每当我更改所选项目时,这都不会通过。
有什么建议吗?
杰伦
确保类型匹配,即如果您的 DefaultSendDay
来源 属性 是 byte
,DayListModel
的 Id
也应该是byte
.
如果还是不行,尝试将字段转换为属性,将结构转换为 class:
public class DayListModel
{
public byte Id { get; set; }
public string DayName { get; set; }
}
这是我的建议,以及我是如何让它发挥作用的:
就像@mm8 说的,你应该把
DayListModel
做成 class 而不是结构,然后把 fields into properties. If you don't use properties, theDisplayMemberPath
doesn't work. Some stuff doesn't work with structs (like using==
to compare) 变成 fields into properties. If you don't use properties, theDisplayMemberPath
doesn't work. Some stuff doesn't work with structs (like using==
to compare) 所以我会用 class 在这种情况下。你的对象在你的视图模型中是可观察的吗?我不知道 telerik,但您应该调用某种
PropertyChanged
方法,以便 UI 可以更新。如果你使用像 MVVM light 这样的框架,它有ObservableObject
s 和RaisePropertyChanged
方法。
这是一个工作示例:
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>
截图: