Почему вылазят ошибки при запуске Error: 2 и Error: 40?
При запуске второй формы, вылазят следующие ошибки (представлены ниже). Как можно их решить и какова их причина появления? Пробовал переделывать поля в свойства (на одном из форумов было как решение) и отписываться от БД, по другому конвертировать изображение, но ошибки как были, так и остались.
System.Windows.Data Error: 40 : BindingExpression path error: 'Image' property not found on 'object' ''Product' (HashCode=4281684)'. BindingExpression:Path=Image; DataItem='Product' (HashCode=4281684); target element is 'ImageBrush' (HashCode=13077833); target property is 'ImageSource' (type 'ImageSource')
System.Windows.Data Error: 40 : BindingExpression path error: 'Value' property not found on 'object' ''Product' (HashCode=4281684)'. BindingExpression:Path=Value; DataItem='Product' (HashCode=4281684); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Name' property not found on 'object' ''Product' (HashCode=4281684)'. BindingExpression:Path=Name; DataItem='Product' (HashCode=4281684); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=Image; DataItem=null; target element is 'ImageBrush' (HashCode=2460041); target property is 'ImageSource' (type 'ImageSource')
Код XAML
<Image x:Name="_imgProduct" HorizontalAlignment="Left" Height="250" VerticalAlignment="Top" Width="248" Margin="66,37,0,0" Stretch="Fill" Grid.Column="2" Grid.RowSpan="4"/>
<Button x:Name="_btnSelectProductImage" Content="Select Image" Style="{StaticResource MaterialDesignFlatMidBgButton}" materialDesign:ShadowAssist.ShadowDepth="Depth0" materialDesign:ButtonAssist.CornerRadius="10" FontSize="18" HorizontalAlignment="Left" Margin="100,11,0,0" VerticalAlignment="Top" TabIndex="6" Width="172" Height="44" Grid.Column="2" Grid.Row="4" Click="OnSelectProductImageClick"/>
<Button x:Name="_btnOK" Content="OK" Grid.Column="1" Grid.Row="5" Height="44" HorizontalAlignment="Center" materialDesign:ShadowAssist.ShadowDepth="Depth0" materialDesign:ButtonAssist.CornerRadius="10" FontSize="18" VerticalAlignment="Center" Width="112" Style="{StaticResource MaterialDesignFlatMidBgButton}" TabIndex="7" Click="OnBtnOKClick"/>
<Button x:Name="_btnCancel" Content="Cancel" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="5" Height="44" materialDesign:ShadowAssist.ShadowDepth="Depth0" materialDesign:ButtonAssist.CornerRadius="10" FontSize="18" HorizontalAlignment="Left" Margin="262,0,0,0" VerticalAlignment="Center" Width="112" Style="{StaticResource MaterialDesignFlatMidBgButton}" TabIndex="8" Click="OnBtnCancelClick"/>
Код, который может влиять на появление ошибок
private void AddFormLoaded(object sender, RoutedEventArgs e)
{
using MuseDBContext db = new();
_cbxCategory.ItemsSource = (List<Category>?)db.Category
.Select(c => new Category
{
CategoryId = c.CategoryId,
CategoryName = c.CategoryName
}).ToList();
_cbxCategory.SelectedValuePath = "CategoryId";
_cbxCategory.DisplayMemberPath = "CategoryName";
}
private void OnSelectProductImageClick(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new ();
dlg.FileName = "";
dlg.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" +
"JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
"Portable Network Graphic (*.png)|*.png";
if (dlg.ShowDialog() == true)
{
_imgUrl = dlg.FileName;
_imgProduct.Source = new BitmapImage(new Uri(dlg.FileName));
}
}
private void AddData()
{
bool luck = false;
using MuseDBContext db = new();
try
{
foreach (var prodName in (from product in db.Product select product.ProductName).ToList())
{
if (prodName == _txtName.Text)
{
luck = false;
MessageBox.Show("The name already exists");
break;
}
else
{
luck = true;
}
}
if (luck)
{
db.Product.Add(new Product
{
ProductImgUrl = _imgUrl,
ProductName = _txtName.Text,
ProductDescr = _txtDescription.Text,
ProductPrice = decimal.Parse(_txtPrice.Text),
ProductCount = int.Parse(_txtCount.Text),
ProductCategory = Convert.ToInt32(_cbxCategory.SelectedValue),
});
}
}
catch
{
luck = false;
MessageBox.Show("Wrong data");
}
if (luck)
{
db.SaveChanges();
MessageBox.Show("Product added");
Close();
}
}