我正在尝试制作一个非常简单的实体框架代码优先项目,但无法解决“调用目标已抛出异常”的问题。当我试图从我的数据库中检索数据时。
我有一个独特的课程:
public class TestStylo
{
[Key]
public int TestStyloID { get; set; }
StyloType styloType { get; set; }
public string name;
public TestStylo(StyloType styloType, string name)
{
this.styloType = styloType;
this.name = name;
}
public StyloType getTypeStylo{
get { return styloType; }
}
}
public enum StyloType
{
pen,
wood,
gold
}
Run Code Online (Sandbox Code Playgroud)
我的模型是:
public class Model1 : DbContext
{
public Model1()
: base("name=Model1")
{
}
public DbSet<TestStylo> MyStylos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的 App.Config 是:
<configuration>
<configSections> …Run Code Online (Sandbox Code Playgroud) 我有一个很奇怪的问题,与工作时ListBox的一个WPF程序。我已经定义了我的ListBox SelectionModeas Single,但看起来即使在视觉上我无法选择多个项目,它也会发生在代码中。可以在这里看到:

在尝试解决方法时,我尝试使用 UnselectAll 函数,但奇怪的是,它仍然选择了一个项目:

作为最后的礼物,一旦选择了多个项目,如果我尝试单击我在代码中选择的未选择的可视化项目,我的应用程序会因 System.ArgumentException 而崩溃(我想它会尝试选择我的项目,该项目已被选中)事实上,它会因为添加一个完美的副本而崩溃?)

我查看了ListBox 甚至在具有相同问题的SelectionMode="Single"中选择了许多项目,但在我的情况下,我无法在视觉上选择多个项目,而且我的项目完全不同,所以它并没有真正的帮助。
我在 ListBox.Click 上没有任何自定义行为,这可能会弄乱某些东西......
myListBox.ItemsSource绑定到一个项目列表。我会在调用useItem它时减少所选项目的属性值,但它仍然是同一个项目(我不会重新创建它/删除它然后再次添加它)
我的问题来自哪里?我该如何解决?
我试图更好地了解事件及其处理程序的工作方式,但我不明白为什么在引发事件时通常更喜欢引发相同的事件,即我们的事件本身。更具体地说,在查看 msdn 文档(https://msdn.microsoft.com/en-us/library/db0etb8x.aspx)时,它看起来像这样:
class Counter
{
private int threshold;
private int total;
public Counter(int passedThreshold)
{
threshold = passedThreshold;
}
public void Add(int x)
{
total += x;
if (total >= threshold)
{
ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
args.Threshold = threshold;
args.TimeReached = DateTime.Now;
OnThresholdReached(args);
}
}
protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)
{
EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;
}
Run Code Online (Sandbox Code Playgroud)
我不明白的是为什么在OnThresholdReached函数中创建了“处理程序” ,而不是
protected virtual void …Run Code Online (Sandbox Code Playgroud) 我的设计师在 MVVM 项目上遇到了问题。
我有TreeView一个自定义DataTemplate:
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Name="img" Width="20" Height="20" Stretch="Fill"
Source="{Binding
RelativeSource={RelativeSource
Mode=FindAncestor,
AncestorType={x:Type TreeViewItem}},
Path=Header,
Converter={StaticResource HeaderToImageConverter}}"
/>
<TextBlock Text="{Binding}" Margin="5,0" />
</StackPanel>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
资源声明:
<Window x:Class="BlobWorld.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:Core="clr-namespace:BlobWorld;assembly="
xmlns:helper="clr-namespace:BlobWorld.Helper"
mc:Ignorable="d"
Title="MainWindow" Height="350.459" Width="746.561"
DataContext="{DynamicResource MainWindowViewModel}">
<Window.Resources>
<helper:HeaderToImageConverter x:Key="HeaderToImageConverter"/>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)
我的转换器是:
public class HeaderToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((value as string).Contains(@"."))
{
Uri uri = new Uri("pack://application:,,,/images/File.png");
BitmapImage source …Run Code Online (Sandbox Code Playgroud) c# ×4
wpf ×2
code-first ×1
designer ×1
events ×1
ioexception ×1
listbox ×1
listboxitem ×1
mvvm ×1