gpt4 book ai didi

wpf - 数据绑定(bind) ComboBox.SelectedItem 时,Int32Converter 无法从 'MyNamespace.MyEnum' 转换

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

对于撇油器。问题位于粗体文本之后的底部附近。

我正在对 ComboBox 进行数据绑定(bind)使用 ObjectDataProvider 转换为枚举类型. ObjectDataProvider很简单。

<ObjectDataProvider x:Key="FaultAreaValues" 
ObjectType="{x:Type FaultTrackCoreDBValues:FaultAreas}"
MethodName="GetValues">

<ObjectDataProvider.MethodParameters>
<x:Type TypeName="FaultTrackCoreDBValues:FaultAreas" />
</ObjectDataProvider.MethodParameters>

</ObjectDataProvider>

我数据绑定(bind) ComboBox通过 ItemsSource ,这也很简单。
ItemsSource="{Binding Source={StaticResource FaultAreaValues}}"

所有这一切都很好。 ComboBox用来自枚举的正确值填充。我也在使用 MVVM 模式,所以 View 的 DataContext设置为其关联的 ViewModel 的实例,看起来(修剪)像这样。
public class ViewModel : INotifyPropertyChanged
{
private IFault _Fault;

public IFault Fault {
get {
return _Fault;
}
set {
_Fault = value;
OnPropertyChanged("Fault");
}
}
}

和模型:
public interface IFault
{
FaultAreas Area { get; set; }
}

public partial class Fault : IFault, INotifyPropertyChanged
{
FaultAreas IFault.Area {
get {
return (FaultAreas)Area;
}
set {
Area = (Int32)value;
}
}
}

需要注意的是 Fault是一个部分类,实际上是一个 Entity在实体数据模型中。 因为 Entity 不支持 .NET 4.5 和 Entity Framework 4.5 之前的枚举,所以我使用显式接口(interface)来添加该支持,并将所有内容与了解有关 Entity 对象的任何内容完全分离。

这是实际的 SelectedItem捆绑:
SelectedItem="{Binding Fault.Area}"

问题是 ComboBox 的选择从不最初设置,更改选择会导致装饰器显示,并导致以下异常:

System.Windows.Data Error: 23 : Cannot convert 'Behavior' from type 'FaultAreas' to type 'System.Int32' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: Int32Converter cannot convert from FaultTrack.Core.DBValues.FaultAreas. at System.ComponentModel.TypeConverter.GetConvertFromException(Object value) at System.ComponentModel.TypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)'



我不确定是什么原因造成的。我可以通过绑定(bind)到 FaultAreas 来解决错误属性直接在 ViewModel 上,但这不是我想要做的。我想绑定(bind)到模型的实例 IFault ,并了解为什么会发生此错误。

最佳答案

当我稍微改变你的代码时,它对我有用。

public partial class Fault : IFault, INotifyPropertyChanged
{
private FaultAreas _area;
public FaultAreas Area
{
get { return (FaultAreas)_area; }
set
{
_area= value;
OnPropertyChanged("Area");
}
}
}

我可以看到初始值并且绑定(bind)所选项目也可以。我只是更改接口(interface)实现并调用 OnPropertyChanged("Area"); .

编辑:它也适用于显式接口(interface)实现
public partial class Fault : IFault, INotifyPropertyChanged
{
private int Area;

public Fault()
{
Area = 0;//default?
}

FaultAreas IFault.Area
{
get{ return (FaultAreas)Area; }
set
{
Area = (int)value;
OnPropertyChanged("Area");
}
}
}

绑定(bind)如下所示:
 SelectedItem="{Binding Path=Fault.(local:IFault.Area)}"

关于wpf - 数据绑定(bind) ComboBox.SelectedItem 时,Int32Converter 无法从 'MyNamespace.MyEnum' 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11026543/

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