【发布时间】:2016-06-17 06:46:49
【问题描述】:
我需要将 ComboBox 绑定到枚举值,并遵循了一些想法。我最终找到了我能找到的最简单的解决方案,它应该满足我的所有需求,除了它不能完全工作,但它应该......
这是我正在做的事情: 我需要 DataGrid 单元格中的 ComboBox,所以我为此定义了一个 DataGridTemplateColumn
<DataGridTemplateColumn MinWidth="150" Header="Data-Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding SelectedDataType,Mode=TwoWay}" ItemsSource="{Binding DataTypeValues}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
在我的 ViewModel 中填充枚举值
public IEnumerable<MyType> DataTypeValues
{
get
{
return Enum.GetValues(typeof(MyType)).Cast<MyType>().ToList<MyType>();
}
}
并且具有所选值的属性
MyType _selectedType;
public MyType SelectedDataType
{
get { return _selectedType; }
set
{
_selectedType = value;
OnPropertyChanged();
}
}
ComboBox 按我的预期填充了我的枚举值,但是当我选择另一个值时,不会调用 SelectedDataType 的设置器,这意味着我不知道当前选择了哪个项目。我也尝试使用 SelectedItem 绑定而不是 SelectedValue,但这也不起作用。
我在这里缺少什么?
编辑:错字
编辑2: --------更新--------
现在我创建了一个示例 WPF 应用程序来重现我的问题。只使用了那里的相关代码部分,并且存在同样的问题。以下是该示例的完整源代码:
Xaml 文件:
<Window x:Class="WpfApplication2.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:WpfApplication2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Parameters}" AutoGenerateColumns="False" SelectedItem="{Binding SelectedParameter, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTemplateColumn MinWidth="150" Header="Data-Type">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding DataTypeValues}" SelectedItem="{Binding SelectedType, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
还有.cs文件:
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var vm = new Viewmodel();
DataContext = vm;
}
}
class Viewmodel : ViewModelBase
{
public Viewmodel()
{
Parameters = new ObservableCollection<Parameter>();
Parameters.Add(new Parameter());
}
public ObservableCollection<Parameter> Parameters
{
get; private set;
}
public Parameter SelectedParameter { get; set; }
}
class Parameter : ViewModelBase
{
MyType _selectedType;
public MyType SelectedType
{
get { return _selectedType; }
set
{
_selectedType = value;
OnPropertyChanged();
}
}
public IEnumerable<MyType> DataTypeValues
{
get { return Enum.GetValues(typeof(MyType)).Cast<MyType>(); }
}
}
public enum MyType
{
INT,
DOUBLE,
REAL,
STRING
}
public class ViewModelBase : INotifyPropertyChanged
{
protected virtual void OnPropertyChanged(bool validateFields = true, [CallerMemberName] string propertyName = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
【问题讨论】:
-
我尝试了更多变体,但似乎问题在于 DataGrid 中 ComboBox 的使用。当我在应用程序的另一部分使用 ComboBox 时,上面的 viewmodel 代码可以很好地工作。但是,当我定义 DataGridColumn 的 CellTemplate 并在那里使用 ComboBox 以及后面发布的代码时,与所选值的绑定不像描述的那样工作......有什么想法吗?我的 XAML 出了什么问题?
-
现在检查。我已经更新了答案。它在我这边工作正常。