在我的WPF MVVM项目中,我收到此错误:
InvalidCastException:无法转换类型' Mocks.DDD_AutoRadio_General_Audio_AudioFile_130_13063066'输入' DDD.AutoRadio.General.Audio.AudioFile'。
WPF MainWindow有一个自定义UserControl
。
简化班AudioFile
:
namespace DDD.AutoRadio.General.Audio
{
[DataContract]
public class AudioFile : INotifyPropertyChanged
{
private string name;
/// <summary>
/// Name of the Mp3
/// </summary>
[DataMember]
public string FilePath
{
get { return name; }
set
{
var oldvalue = name;
if (oldvalue != value)
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("FilePath"));
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
但我不认为这与这堂课有关
我认为自定义UserControl
:
public partial class EditField : UserControl
{
public static DependencyProperty selectedList =
DependencyProperty.Register("DataGridSelectedItems", typeof(OC<AudioFile>), typeof(EditField));
public OC<AudioFile> DataGridSelectedItems
{
get { return (OC<AudioFile>)GetValue(selectedList); }
set { SetValue(selectedList, value); }
}
public EditField()
{
InitializeComponent();
}
private void Collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OC<AudioFile> l = new OC<AudioFile>();
foreach (AudioFile i in Collection.SelectedItems) //'Collection' is a DataGrid
l.Add(i);
DataGridSelectedItems = l;
}
}
注意:OC是基于ObservableColletion<>
类
public class OC<T> : ObservableCollection<T> where T: INotifyPropertyChanged
这是StackTrace
at DDD.AutoRadio.Database.Editor.View.EditField.Collection_SelectionChanged(Object sender, SelectionChangedEventArgs e)
at System.Windows.Controls.SelectionChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.DataGrid.OnSelectionChanged(SelectionChangedEventArgs e)
at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedInfos, List`1 selectedInfos)
at System.Windows.Controls.Primitives.Selector.SelectionChanger.End()
at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(ItemInfo info, Boolean assumeInItemsCollection)
at System.Windows.Controls.Primitives.Selector.SetSelectedToCurrent()
at System.Windows.Controls.Primitives.Selector.SetSynchronizationWithCurrentItem()
at System.Windows.Controls.DataGrid.OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
at System.Windows.Controls.ItemsControl.OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
at System.Windows.DependencyObject.InvalidateProperty(DependencyProperty dp, Boolean preserveCurrentValue)
at System.Windows.Data.BindingExpressionBase.Invalidate(Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.TransferValue(Object newValue, Boolean isASubPropertyChange)
at System.Windows.Data.BindingExpression.Activate(Object item)
at System.Windows.Data.BindingExpression.AttachToContext(AttachAttempt attempt)
at System.Windows.Data.BindingExpression.MS.Internal.Data.IDataBindEngineClient.AttachToContext(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Task.Run(Boolean lastChance)
at MS.Internal.Data.DataBindEngine.Run(Object arg)
at MS.Internal.Data.DataBindEngine.OnLayoutUpdated(Object sender, EventArgs e)
at System.Windows.ContextLayoutManager.fireLayoutUpdateEvent()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.UIElement.UpdateLayout()
答案 0 :(得分:1)
我找到了解决方案:
在自定义UserControl
EditField
中,有一个DependentieProperty
'DataGridItemsSource',其中SelectedItems
来自。{/ p>
我需要做的就是从源中选择对象,而不是从SelectedItems中选择。
(在我的Class
AudioFile中,我还有一个属性'Songcode')
public partial class EditField : UserControl
{
#region DataGridSelectedItems
public static DependencyProperty selectedList =
DependencyProperty.Register("DataGridSelectedItems", typeof(OC<AudioFile>), typeof(EditField));
public OC<AudioFile> DataGridSelectedItems
{
get { return (OC<AudioFile>)GetValue(selectedList); }
set { SetValue(selectedList, value); }
}
#endregion
#region DataGridItemsSource
public static DependencyProperty datagriditemsource =
DependencyProperty.Register("DataGridItemsSource", typeof(OC<AudioFile>), typeof(EditField));
public OC<AudioFile> DataGridItemsSource
{
get { return (OC<AudioFile>)GetValue(datagriditemsource); }
set { SetValue(datagriditemsource, value); }
}
#endregion
public EditField()
{
InitializeComponent();
}
private void Collection_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
OC<AudioFile> l = new OC<AudioFile>();
DataGridSelectedItems.Clear();
foreach (AudioFile i in Collection.SelectedItems) //'Collection' is a DataGrid
l.Add(DataGridItemsSource.Where(x => x.Songcode == i.Songcode || x.FilePath == i.FilePath).First());
If (l.Count > 0)
DataGridSelectedItems = l;
}
}