C
C#2mo ago
TheUnquiet

Name does not exist in the current context

I need to access the Text inside a textBox but its name does not exist. the code is found in the text files
192 Replies
leowest
leowest2mo ago
$details
MODiX
MODiX2mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, what you expect the result to be, what .NET version you are using and what platform/environment (if any) are relevant to your question. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
leowest
leowest2mo ago
x:Name but even better would be if u use bindings
TheUnquiet
TheUnquiet2mo ago
tried that and Name none of them worked
leowest
leowest2mo ago
what is your code behind or viewmodel if u have one
TheUnquiet
TheUnquiet2mo ago
private void SluitButtonClick(object sender, RoutedEventArgs e)
{
bool aanleg, afhaal;
if ((bool)AanlegRadioButton.IsChecked) aanleg = true; else aanleg = false;
if ((bool)AfhaalRadioButton.IsChecked) afhaal = true; else afhaal = false;

Offerte offerte = new(DateTime.Now, int.Parse(KlantIdTextBlock.Text), afhaal, aanleg, GeselecteerdeProducten.Count);
tuincentrumManager.UploadOfferte(offerte);

foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}

Close();
}
private void SluitButtonClick(object sender, RoutedEventArgs e)
{
bool aanleg, afhaal;
if ((bool)AanlegRadioButton.IsChecked) aanleg = true; else aanleg = false;
if ((bool)AfhaalRadioButton.IsChecked) afhaal = true; else afhaal = false;

Offerte offerte = new(DateTime.Now, int.Parse(KlantIdTextBlock.Text), afhaal, aanleg, GeselecteerdeProducten.Count);
tuincentrumManager.UploadOfferte(offerte);

foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}

Close();
}
leowest
leowest2mo ago
and which one it doesnt find
TheUnquiet
TheUnquiet2mo ago
its this line: tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
leowest
leowest2mo ago
AantalTextBox ?
TheUnquiet
TheUnquiet2mo ago
yes I gave it x:Name="AantalTextBox" but the error still presists
leowest
leowest2mo ago
it doesnt exist in your xaml
TheUnquiet
TheUnquiet2mo ago
is there a way to Bind it to Bestelling? I added it <TextBox Width="50" Margin="5" x:Name="AantalTextBox"/> this is the line I added
leowest
leowest2mo ago
what is Bestelling sorry I dont se it on xaml that looks fine
TheUnquiet
TheUnquiet2mo ago
this is the model using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BL.Models { public class Bestelling { public int Offerte_Id; public int Product_Id; public int Aantal_Product; public Bestelling(int offerte_Id, int product_Id, int aantal_Product) { Offerte_Id = offerte_Id; Product_Id = product_Id; Aantal_Product = aantal_Product; } } }
leowest
leowest2mo ago
well u cannot bind fields to XAML u need properties are u using mvvm or only code behind? just trying to understand code behind is when u use xaml.cs for everything
TheUnquiet
TheUnquiet2mo ago
nope
leowest
leowest2mo ago
ok so xaml.cs
TheUnquiet
TheUnquiet2mo ago
yes
leowest
leowest2mo ago
this should work if u build does it work? as it does it says succeed in the build if it does vs might be drunk
TheUnquiet
TheUnquiet2mo ago
I tried Rebuilding
leowest
leowest2mo ago
also your xaml does not have a lot of the things u are showing me SluitButtonClick is not in any button
TheUnquiet
TheUnquiet2mo ago
Yeah it was SluitButtonClick I changed to OpslaanEnSluitButtonClick
leowest
leowest2mo ago
ah ok that one I see I mean then your textbox should work
TheUnquiet
TheUnquiet2mo ago
after building the error is still there
leowest
leowest2mo ago
but in the output window what does it say
TheUnquiet
TheUnquiet2mo ago
ill show you
TheUnquiet
TheUnquiet2mo ago
No description
leowest
leowest2mo ago
in the output window if it says that
leowest
leowest2mo ago
No description
TheUnquiet
TheUnquiet2mo ago
No description
leowest
leowest2mo ago
ok check what file it complains about it should say on the right side
TheUnquiet
TheUnquiet2mo ago
Tuincentrum_UI_x02civxy_wpftmp.csproj im not sure what file this is
TheUnquiet
TheUnquiet2mo ago
I found these
No description
leowest
leowest2mo ago
No description
leowest
leowest2mo ago
it would be like that u see mine says MainWindow.xaml.cs
TheUnquiet
TheUnquiet2mo ago
yes the code behind I sent is for that fike file*
leowest
leowest2mo ago
ok so can u send me the code again updated with exactly what u have now xaml and xaml.cs $paste
MODiX
MODiX2mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
leowest
leowest2mo ago
if possible use this site its better then discord to read
TheUnquiet
TheUnquiet2mo ago
I will
TheUnquiet
TheUnquiet2mo ago
BlazeBin - gdrlxvzpcihi
A tool for sharing your source code with the world!
leowest
leowest2mo ago
ok moment
TheUnquiet
TheUnquiet2mo ago
thank you
leowest
leowest2mo ago
its missing the xaml.cs file ah I see because the textbox is inside a datatemplate that wont work
TheUnquiet
TheUnquiet2mo ago
I had a feeling can you help me get to the TextBox?
leowest
leowest2mo ago
what u should do is using an ObservableCollection and binding
TheUnquiet
TheUnquiet2mo ago
yeah I got the collection
TheUnquiet
TheUnquiet2mo ago
here is the cs file i'm very sorry for not sending it before https://paste.mod.gg/hfxmutjbldzo/0
BlazeBin - hfxmutjbldzo
A tool for sharing your source code with the world!
leowest
leowest2mo ago
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
}
}
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
}
}
and then in your xaml.cs
public ObservableCollection<Bestelling> Products {get;set;}
public ObservableCollection<Bestelling> Products {get;set;}
and then your listbox
TheUnquiet
TheUnquiet2mo ago
okay
leowest
leowest2mo ago
ItemsSource="{Binding Products}"
ItemsSource="{Binding Products}"
what is ProductNaarString
TheUnquiet
TheUnquiet2mo ago
I don't understand using BL.Exceptions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BL.Models { public class Product { public int? Id; public string ProductNaarString => ToString(); private string naam_nl; public string Naam_nl { get { return naam_nl; } set { if (string.IsNullOrWhiteSpace(value)) throw new TuincentrumException("Naam nl is leeg"); naam_nl = value; } } private string naam_w; public string Naam_W {
get { return naam_w; } set { if (string.IsNullOrWhiteSpace(value)) throw new TuincentrumException("Naam w is leeg"); naam_nl = value; } } public double Prijs; public string Beschrijving; public Product(int id, string naam_nl, string naam_W, double prijs, string beschrijving) { Id = id; Naam_nl = naam_nl; Naam_W = naam_W; Prijs = prijs; Beschrijving = beschrijving; } public Product(string naam_nl, string naam_W, double prijs, string beschrijving) { Naam_nl = naam_nl; Naam_W = naam_W; Prijs = prijs; Beschrijving = beschrijving; } public override string ToString() { return $"{Naam_nl} {Naam_W} {Beschrijving} €{Math.Round(Prijs, 2)}"; } } }
leowest
leowest2mo ago
<ListBox ... ItemsSource="{Binding Products}" ...>
TheUnquiet
TheUnquiet2mo ago
could you take a look at the xaml.cs file? I got a link https://paste.mod.gg/hfxmutjbldzo/0
BlazeBin - hfxmutjbldzo
A tool for sharing your source code with the world!
leowest
leowest2mo ago
I will now but what is ProductNaarString
TheUnquiet
TheUnquiet2mo ago
its a property I made to hold the ToString method I had a previous problem with the binding I set on the textBlock
leowest
leowest2mo ago
ah ok Ah wow I see there is a lot of issues because you're mixing binding with code behind and no datacontext
TheUnquiet
TheUnquiet2mo ago
oh, could you help make the code a little better?
leowest
leowest2mo ago
ok let me think for a moment what is easiest to explain
TheUnquiet
TheUnquiet2mo ago
thank you very much
leowest
leowest2mo ago
u dont edit items in the listbox do u?
TheUnquiet
TheUnquiet2mo ago
you can edit the TextBox
leowest
leowest2mo ago
ok what would be the aantal_product in Product?
TheUnquiet
TheUnquiet2mo ago
you mean in Bestelling?
leowest
leowest2mo ago
you're not using bestelling as far as I can see in Product
TheUnquiet
TheUnquiet2mo ago
so I've got products in a collection then im making a Bestelling object for each of those Product objects Bestelling has a property Aantal_product which should have the value of whatever the user has entered in the textbox
leowest
leowest2mo ago
??translate en bestelling ah mm bot doesnt work in threads what bestelling means
TheUnquiet
TheUnquiet2mo ago
'Order_product' this is the object in between Order and Product
leowest
leowest2mo ago
ok so list A is all products list B is the shopping cart?
TheUnquiet
TheUnquiet2mo ago
yes
leowest
leowest2mo ago
ok I will worry about that latter first I will show u how to do this then we work our way for bestelling which might need to change things ok, so what u want to do is: To your xaml change these 2 like these:
<ListBox ItemsSource="{Binding AlleProducten}" Grid.Row="2" Grid.Column="0" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended"/>
<ListBox ItemsSource="{Binding GeselecteerdeProducten}" Grid.Row="2" Grid.Column="2" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox ItemsSource="{Binding AlleProducten}" Grid.Row="2" Grid.Column="0" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended"/>
<ListBox ItemsSource="{Binding GeselecteerdeProducten}" Grid.Row="2" Grid.Column="2" Margin="5" Grid.ColumnSpan="2" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Then in your xaml.cs u want to do this, change these 2 lines from
private ObservableCollection<Product> AlleProducten;
private ObservableCollection<Product> GeselecteerdeProducten;
private ObservableCollection<Product> AlleProducten;
private ObservableCollection<Product> GeselecteerdeProducten;
to
public ObservableCollection<Product> AlleProducten {get;set;}
public ObservableCollection<Product> GeselecteerdeProducten {get;set;}
public ObservableCollection<Product> AlleProducten {get;set;}
public ObservableCollection<Product> GeselecteerdeProducten {get;set;}
Then change this from:
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());
AlleProductenListBox.ItemsSource = AlleProducten;

GeselecteerdeProducten = new ObservableCollection<Product>();
GeselecteerdeProductenListBox.ItemsSource = GeselecteerdeProducten;
Klant = k;
}
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());
AlleProductenListBox.ItemsSource = AlleProducten;

GeselecteerdeProducten = new ObservableCollection<Product>();
GeselecteerdeProductenListBox.ItemsSource = GeselecteerdeProducten;
Klant = k;
}
to
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
DataContext = this;

tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());

GeselecteerdeProducten = new ObservableCollection<Product>();
Klant = k;
}
public NieuweOfferteWindow(Klant k)
{
InitializeComponent();
DataContext = this;

tuincentrumRepository = new TuincentrumRepository(ConfigurationManager.ConnectionStrings["TuincentrumDBConnectionLaptop"].ToString());
tuincentrumManager = new TuincentrumManager(tuincentrumRepository);

DatumTextBlock.Text = DateTime.Now.ToString("dd/MM/yyyy");
KlantIdTextBlock.Text = k.Id.ToString();
KlantNaamTextBlock.Text = k.Naam;
KlantAdresTextBlock.Text = k.Adres;

AlleProducten = new ObservableCollection<Product>(tuincentrumManager.GeefProducten());

GeselecteerdeProducten = new ObservableCollection<Product>();
Klant = k;
}
By doing this we are not actually making use of the DataContext and bindings in xaml
TheUnquiet
TheUnquiet2mo ago
one moment please
TheUnquiet
TheUnquiet2mo ago
I'm getting this error in my .xaml file
No description
leowest
leowest2mo ago
try reloading the designer never seen that error before
TheUnquiet
TheUnquiet2mo ago
yup still there ill reload vscode
leowest
leowest2mo ago
ah vscode :OOF: I would not be able to help u with vscode I dont use it for c#
TheUnquiet
TheUnquiet2mo ago
reloading the app worked no i meant Visual studio im using vs sorry forget sometimes
leowest
leowest2mo ago
visual studio community ok
TheUnquiet
TheUnquiet2mo ago
yes
leowest
leowest2mo ago
yeah I have 0 idea about vscode issues 😛
TheUnquiet
TheUnquiet2mo ago
i have enterprise too, if that one is better
leowest
leowest2mo ago
either should work anyway does it build now?
TheUnquiet
TheUnquiet2mo ago
vscode isnt good for compiling
leowest
leowest2mo ago
ah right I forgot change this too
TheUnquiet
TheUnquiet2mo ago
now I'm having issues with my other functions
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in GeselecteerdeProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Remove(p);
AlleProducten.Add(p);
}
}
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in GeselecteerdeProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Remove(p);
AlleProducten.Add(p);
}
}
The listbox doesnt exist anymore so I cant put any products in
leowest
leowest2mo ago
foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}
foreach (Product p in GeselecteerdeProducten)
{
tuincentrumManager.UploadBestelling(new Bestelling(offerte.Id.Value, p.Id.Value, AantalTextBox.Text));
}
u would change AantalTextBox.Text to either Naam_nl or u would need ot make
public string ProductNaarString => ToString();
public string ProductNaarString => ToString();
A get;set; to use that
TheUnquiet
TheUnquiet2mo ago
I need the textbox so the user can put the amount of the product
leowest
leowest2mo ago
oh so its mean to be a quantity ok now it makes mose sense
TheUnquiet
TheUnquiet2mo ago
sorry I forgot to explain thank you for your patience
leowest
leowest2mo ago
then its best if u have a property in Product that says quantity int then u can bind it
TheUnquiet
TheUnquiet2mo ago
Bestelling has that property, should I use Bestelling instead of Product?
leowest
leowest2mo ago
yes it would be better so u just set it to 1 by default and user can modify it on the 2nd listbox
TheUnquiet
TheUnquiet2mo ago
okay
leowest
leowest2mo ago
and add the x:Name="AlleProductenListBox" back to your lists so u can use them from the button that should be all I guess u can change the
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Width="50" Margin="5" x:Name="AantalTextBox"/>
</StackPanel>
</StackPanel>
</DataTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Width="50" Margin="5" x:Name="AantalTextBox"/>
</StackPanel>
</StackPanel>
</DataTemplate>
to
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5" />
</StackPanel>
</StackPanel>
</DataTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Aantal:" Margin="5"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5" />
</StackPanel>
</StackPanel>
</DataTemplate>
and it should reflect the changes in your ObservableCollection so it will update Quantity
TheUnquiet
TheUnquiet2mo ago
got it I also added this back : AlleProductenListBox.ItemsSource = AlleProducten;
leowest
leowest2mo ago
and then u would need to add the name to your bestelling no need if u have it the way I gave u earlier setting it from xaml is better for visibility
TheUnquiet
TheUnquiet2mo ago
The Binding takes care of it?
leowest
leowest2mo ago
yes
TheUnquiet
TheUnquiet2mo ago
cool
leowest
leowest2mo ago
let me know if it works of if u have issues still
TheUnquiet
TheUnquiet2mo ago
yeah I have a question so to access the quantity what's the syntax?
leowest
leowest2mo ago
so u would have say
public ObservableCollection<Bestelling> GeselecteerdeProducten {get;set;}
public ObservableCollection<Bestelling> GeselecteerdeProducten {get;set;}
And if u loop
foreach (var item in GeselecteerdeProducten)
{
//item.Quantity
}
foreach (var item in GeselecteerdeProducten)
{
//item.Quantity
}
GeselecteerdeProducten is the 2nd listbox right?
TheUnquiet
TheUnquiet2mo ago
yes to transfer the products from the first listbox to the second I cant just remove and add them right? Since the types are Product and Bestelling instead of Product and Product
leowest
leowest2mo ago
U can remove if u want, u would just have to keep track of the items to add back
TheUnquiet
TheUnquiet2mo ago
this is the method that adds the products from the first list to the other
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in AlleProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Add(p);
AlleProducten.Remove(p);
}
}
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
List<Product> producten = new List<Product>();
foreach (Product p in AlleProductenListBox.SelectedItems) producten.Add(p);
foreach (Product p in producten)
{
GeselecteerdeProducten.Add(p);
AlleProducten.Remove(p);
}
}
leowest
leowest2mo ago
You could keep a list of like: private List<Product> removedProducts = new List<Product>(); if u want to remove and add items back and just use the item Id to compare from bestelling to product
TheUnquiet
TheUnquiet2mo ago
should I just rewrite the working of everything? or is it better to get rid of the textbox and have users just add as many using the button instead?
leowest
leowest2mo ago
the textbox is fine
TheUnquiet
TheUnquiet2mo ago
then I can just count the amount of times the product appears in the list
leowest
leowest2mo ago
u can even make it a numeric up down box visually having the quantity on the cart is better
TheUnquiet
TheUnquiet2mo ago
I understand
leowest
leowest2mo ago
I just dont see any need to remove the product from the list of products if the user add the same product just check the id if it exists in the list of cart
TheUnquiet
TheUnquiet2mo ago
this makes things way to complicated
leowest
leowest2mo ago
just add+ 1 instead of adding a new item
TheUnquiet
TheUnquiet2mo ago
I thought it would be best so that users cant add the same one again so I'll check if the selected item is already in the second list and if it is i'll add + to the textbox?
leowest
leowest2mo ago
that is one way, u dont add to the box u add to the item item.Quantity += 1;
TheUnquiet
TheUnquiet2mo ago
right one more thing
leowest
leowest2mo ago
but you can remove items if u prefer u would just have an extra list keeping the items removed so u can add it back later it would not be complicated its just 1 extra check
TheUnquiet
TheUnquiet2mo ago
whenever I transfer one product to the other listbox the product shows up in the listbox how can i achieve this now that I'm using Bestelling as a type instead of Product remove from AlleProducten?
leowest
leowest2mo ago
yeah
private List<Product> removedProducts = new List<Product>();
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
private List<Product> removedProducts = new List<Product>();
private void VerwijderProductenButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
something like this to add product back to product u check product id from bestelling against the product id on product
TheUnquiet
TheUnquiet2mo ago
right, how do I show the Product in the ListBox (the second listBox)?
leowest
leowest2mo ago
when u add item to GeselecteerdeProducten it auto updates the list
TheUnquiet
TheUnquiet2mo ago
ill run it
leowest
leowest2mo ago
is it not showing?
TheUnquiet
TheUnquiet2mo ago
no
leowest
leowest2mo ago
u change this right
TheUnquiet
TheUnquiet2mo ago
yeah cause the type right now is Bestelling
leowest
leowest2mo ago
can u show me your current code on xaml and xmal.cs
TheUnquiet
TheUnquiet2mo ago
yes this one is throwing errors
TheUnquiet
TheUnquiet2mo ago
No description
leowest
leowest2mo ago
yes I need to see the code to see the changes u did
TheUnquiet
TheUnquiet2mo ago
BlazeBin - sycdjoacwtbv
A tool for sharing your source code with the world!
leowest
leowest2mo ago
u did not change things like how I told u :/
leowest
leowest2mo ago
No description
TheUnquiet
TheUnquiet2mo ago
oh didnt see that
leowest
leowest2mo ago
that is why it was not showing also line 50 no need FirstOrDefault(x => x.Id == c.Id); this was an example u have to change Id to the actual Id names in bestelling and product
TheUnquiet
TheUnquiet2mo ago
yup I have a question what's p?
leowest
leowest2mo ago
Product
TheUnquiet
TheUnquiet2mo ago
this line is throwing an error: foreach (var c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(p); p does not exist in the current context
leowest
leowest2mo ago
ah my bad that one I forgot to change to c that line cart.Add(c);
TheUnquiet
TheUnquiet2mo ago
No description
TheUnquiet
TheUnquiet2mo ago
so all the products are already in the second listbox it should be that a user can add one and then remove one
leowest
leowest2mo ago
it should not because you're initializing it to empty how are u adding items to it, its not in the code u sent me
TheUnquiet
TheUnquiet2mo ago
this is the method that adds items
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (Bestelling c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product_Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
private void VoegProductButtonClick(object sender, RoutedEventArgs e)
{
var cart = new List<Bestelling>();
foreach (Bestelling c in GeselecteerdeProductenListBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product_Id);
GeselecteerdeProducten.Remove(c);
AlleProducten.Add(p);
removedProducts.Remove(p);
}
}
leowest
leowest2mo ago
no that is to remove
TheUnquiet
TheUnquiet2mo ago
oh
leowest
leowest2mo ago
there is no code to add items to GeselecteerdeProducten so how is it filled
TheUnquiet
TheUnquiet2mo ago
the listbox is full without me adding any products i have no clue
leowest
leowest2mo ago
what is your xaml u didnt send me
TheUnquiet
TheUnquiet2mo ago
BlazeBin - hqquteseflpq
A tool for sharing your source code with the world!
leowest
leowest2mo ago
well u have no code filling the 2nd list
TheUnquiet
TheUnquiet2mo ago
hey its good now 💀 I just ran the app again so to recap to add a product I should?
leowest
leowest2mo ago
do the inverse of how u remove items add item to removedProducts add bestelling to cart list and remove item from products list
TheUnquiet
TheUnquiet2mo ago
i'm so confused what's the purpose of the removedProducts List?
leowest
leowest2mo ago
it keeps the items u have removed from products so u can add it back when u remove them from cart because cart is a different type Bestelling != Product
TheUnquiet
TheUnquiet2mo ago
yes
leowest
leowest2mo ago
u keep a list that intermediate both which is the removedProducts
TheUnquiet
TheUnquiet2mo ago
okay, but that list is Product too?
leowest
leowest2mo ago
yes
leowest
leowest2mo ago
No description
TheUnquiet
TheUnquiet2mo ago
im still a little confused
leowest
leowest2mo ago
about what its the same thing u were doing before except u have a separated list to store the removed products that is not visible to the xaml u add item to bestelling u add item to removedProducts u remove item from Products
TheUnquiet
TheUnquiet2mo ago
right, I'm confused about how i'll show the products in the secod listbox
leowest
leowest2mo ago
by adding the bestelling to GeselecteerdeProducten? similar to how u do in your button OpslaanEnSluitenButtonClick
TheUnquiet
TheUnquiet2mo ago
yeah that makes sene snese sense but, I need to show product names
leowest
leowest2mo ago
and it will
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Aantal_Product}" Width="50" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Aantal_Product}" Width="50" Margin="5"/>
</StackPanel>
if Aantal_Product is quantity and ProductNaarString is the product name in Bestelling it will work just fine if its not u need to add a name property to bestelling or a refence to Product so u can do say
TheUnquiet
TheUnquiet2mo ago
even tho ProductNaarString is from the Product class?
leowest
leowest2mo ago
public Product ProductInfo {get;set;} so u can use ProductInfo.ProductNaarString whichever u think its best it can be as simple as
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}
public string DisplayName {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product, string displayName)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
DisplayName = displayName
}
}
public class Bestelling
{
public int Offerte_Id {get;set;}
public int Product_Id {get;set;}
public int Aantal_Product {get;set;}
public string DisplayName {get;set;}

public Bestelling(int offerte_Id, int product_Id, int aantal_Product, string displayName)
{
Offerte_Id = offerte_Id;
Product_Id = product_Id;
Aantal_Product = aantal_Product;
DisplayName = displayName
}
}
TheUnquiet
TheUnquiet2mo ago
is there really no other way than everything we're doing?
leowest
leowest2mo ago
and u change ProductNaarString to DisplayName u could add quantity to product and keep everything as it was before if u want
TheUnquiet
TheUnquiet2mo ago
then there is no use in having the Bestelling class
leowest
leowest2mo ago
but doesn't sound like a Product responsibility to hold the quantity the user is buying
TheUnquiet
TheUnquiet2mo ago
yeah
leowest
leowest2mo ago
the problem here is how you are handling things normally you have Product and u have Cart Cart will usually have some references of Product
TheUnquiet
TheUnquiet2mo ago
should I make a nother class?
leowest
leowest2mo ago
i.e.:
public class Cart
{
public int Id {get;set;}
public int ProductId {get;set;}
public Product Product {get;set;}
public int Quantity {get;set;}
}
public class Cart
{
public int Id {get;set;}
public int ProductId {get;set;}
public Product Product {get;set;}
public int Quantity {get;set;}
}
TheUnquiet
TheUnquiet2mo ago
I can check the product by its id using a method that asks the db is that better in your opinion?
leowest
leowest2mo ago
I dont think that is relevant here since u already have a list with all products but having a reference of Product in your cart
TheUnquiet
TheUnquiet2mo ago
cant it help with showing the name?
leowest
leowest2mo ago
allows the flexibity of u being able to use its name etc so u could do say Product.Name in the binding as an example now it depends if you're using those models for something else i.e.: are u using EFCore? if u are then it becomes a bit more complex
TheUnquiet
TheUnquiet2mo ago
nope ADO
leowest
leowest2mo ago
Ado? ok then yes that is fine
TheUnquiet
TheUnquiet2mo ago
alright
leowest
leowest2mo ago
in this case here all u would do is
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding Product.ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="5">
<TextBlock Text="{Binding Product.ProductNaarString}"/>
<TextBlock Text="Aantal"/>
<TextBox Text="{Binding Quantity}" Width="50" Margin="5"/>
</StackPanel>
TheUnquiet
TheUnquiet2mo ago
I got a question, what's the difference between Product.ProductNaarString and ProductNaarString?
leowest
leowest2mo ago
because the 2nd list is Cart so we are accessing the Product reference in the Cart item
TheUnquiet
TheUnquiet2mo ago
so to add I should?
leowest
leowest2mo ago
👆
TheUnquiet
TheUnquiet2mo ago
alright, I'll figure the rest out on my own thank you for all the help
leowest
leowest2mo ago
leowest
leowest2mo ago
public record Product(int Id, string Name, decimal Price);
public record Cart(int Id, Product Product, int Quantity);
public record Product(int Id, string Name, decimal Price);
public record Cart(int Id, Product Product, int Quantity);
and
public ObservableCollection<Product> Products { get; set; }
public ObservableCollection<Cart> Cart { get; set; }
private List<Product> removedProducts = new List<Product>();

public Window1()
{
Products = new ObservableCollection<Product>
{
new Product(1, "Boots", 120),
new Product(1, "Pants", 90),
new Product(1, "Shirt", 81),
new Product(1, "Gloves", 22),
new Product(1, "Hat", 12),
};

Cart = new ObservableCollection<Cart>();
InitializeComponent();
DataContext = this;
}

private void AddAll(object sender, RoutedEventArgs e)
{
var products = new List<Product>();
foreach (Product p in ProductBox.SelectedItems) products.Add(p);
foreach (Product p in products)
{
Cart.Add(new Cart(1, p, 0));
removedProducts.Add(p);
Products.Remove(p);
}
}

private void RemoveAll(object sender, RoutedEventArgs e)
{
var cart = new List<Cart>();
foreach (Cart c in CartBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product.Id);
Cart.Remove(c);
Products.Add(p);
removedProducts.Remove(p);
}
}
}
public ObservableCollection<Product> Products { get; set; }
public ObservableCollection<Cart> Cart { get; set; }
private List<Product> removedProducts = new List<Product>();

public Window1()
{
Products = new ObservableCollection<Product>
{
new Product(1, "Boots", 120),
new Product(1, "Pants", 90),
new Product(1, "Shirt", 81),
new Product(1, "Gloves", 22),
new Product(1, "Hat", 12),
};

Cart = new ObservableCollection<Cart>();
InitializeComponent();
DataContext = this;
}

private void AddAll(object sender, RoutedEventArgs e)
{
var products = new List<Product>();
foreach (Product p in ProductBox.SelectedItems) products.Add(p);
foreach (Product p in products)
{
Cart.Add(new Cart(1, p, 0));
removedProducts.Add(p);
Products.Remove(p);
}
}

private void RemoveAll(object sender, RoutedEventArgs e)
{
var cart = new List<Cart>();
foreach (Cart c in CartBox.SelectedItems) cart.Add(c);
foreach (var c in cart)
{
var p = removedProducts.FirstOrDefault(x => x.Id == c.Product.Id);
Cart.Remove(c);
Products.Add(p);
removedProducts.Remove(p);
}
}
}
this is just a basic example ofc your data comes from the database etc anyway I got to go for now