My Listview is not updating where i send data in another windows, but update when send data in same windows. here is my main window
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
CNotes.writeLn("Jacob");
}
private void button2_Click(object sender, RoutedEventArgs e)
{
Window1 spl = new Window1();
spl.Show();
}
}
}
and here is my window listview
public partial class Window1 : Window
{
ObservableCollection<Person> people = new ObservableCollection<Person();
public Window1()
{
InitializeComponent();
people.Add(new Person() { Name = "John" });
people.Add(new Person() { Name = "Jack" });
// Data bind to the listview,
this.lvRcvd.ItemsSource = people;
}
public void writeLn(string s)
{
this.people.Add(new Person { Name = s });
}
private void button1_Click(object sender, RoutedEventArgs e)
{
this.people.Add(new Person { Name = "Jacob" });
}
}
public class Person { public string Name { get; set; } }
static public class CNotes
{
static Window1 fm = new Window1();
static public void writeLn(string s)
{
fm.writeLn(s);
}
}
when i click mainwindow button1 , listview not update .
but when i click window1 button1 , listview update
how to update listview in window1 when i click mainwindow button1 ?
Change this:
static public void writeLn(string s)
{
fm.writeLn(s);
}
To this:
static public void writeLn(string s)
{
Window1 fm = Application.OpenForms.OfType<Window1>().Take(1).SingleOrDefault();
if (fm != null)
fm.writeLn(s);
}
What you are doing is creating a new instance of form Window1 instead of accessing the open Window1 which you would like to update.
Edit:
Since this is a WPF, try using Application.Current. More specifically, Application.Current.Windows.
Related
I have a "MainForm" and a "GraphicsForm". Clicking "New" on the main form, a "GraphicsForm" will be created.
The problem is that when I create multiple "GraphicsForm", and when I want to save the content of one of the "GraphicsForm", I need to clicking "Save" on the "MainForm" and the program will save the content of the active "GraphicsForm" to a file, I don't know how to pass the content of this "GraphicsForm" to "MainForm" for storage.
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm();
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // Problem here
}
private void Open_Click(object sender, EventArgs e)
{
... // Problem here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//StorageDoc is a class to manage all the graphics drawn by the user in the form.
private StorageDoc _storageDoc = new StotageDoc();
public GraphicsForm()
{
InitializeComponent();
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
Because MainForm is a MDI form, it is easy to use ActiveMdiChild to get the active child form.
class MainForm : Form
{
public void OnSaveButtonClick(object sender, EventArgs e)
{
if(ActiveMdiChild is GraphicsForm g)
Save(g);
}
}
I'm sure this has been answered before but basically, you pass in an instance of the 'data storage' to the new form.
interface ISaveForm
{
void Save();
}
class MainForm
{
private DataStorage _dataStorage;
private ICollection<ISaveForm> _forms = new List<ISaveForm>();
public void OnNew()
{
var subForm = new GraphicsForm(_dataStorage);
subForm.Show();
_forms.Add(subForm);
}
public void OnSave()
{
foreach(var form in _forms)
{
form.Save();
}
}
}
class GraphicsForm : Form,ISaveForm
{
private DataStorage _dataStorage;
public GraphicsForm(DataStorage dataStorage)
{
_dataStorage = dataStorage;
}
public void Save()
{
}
}
I developed an app, where the user sects on order from a list. If the order is selected, the EditOrderWindow shows. From this window, the user can add items to the Order by. This is done in a sub dialog AddItemWindow. Works fine for the first order.
However, if a second order is edited, the AddItemWindow shows again after its been closed. For the third order edited it shows three times aso.
I couldn't figure out why this happens, and how to prevent it.
ViewModel
public class ViewModel
{
public ICommand EditOrderCommand { get; set; }
public ICommand AddItemCommand { get; set; }
public event EventHandler<int> EditOrder;
public event EventHandler<int> AddItem;
public event EventHandler<int> ItemAdded;
public ViewModel()
{
//Is fired when an item in the datagrid in Control OrderList is doubleclicked
EditOrderCommand = new ICommand();
EditOrderCommand.Executed += EditOrderCommand_Executed();
//Is fired when the users click on add in EditOrderWindow
AddItemCommand = new ICommand();
AddItemCommand.Executed += AddItemCommand_Executed();
}
private void EditOrderCommand_Executed(object sender, EventArgs e)
{
if (SelectedOrder != null)
{
EditOrder(this, SelectedOrder.Id);
}
}
private void AddItemCommand_Executed(object sender, EventArgs e)
{
AddItem(this, new EventArgs());
}
}
Control: OrderList
public partial class OrderList : UserControl
{
ViewModel vm;
public OrderList()
{
InitializeComponent();
//DataContext is set in xmal
vm = (ViewModel)this.DataContext;
vm.EditOrder += Vm_EditOrder;
}
private void Vm_EditOrder(object sender, int e)
{
EditOrderWindow s = new EditOrderWindow(vm);
s.ShowDialog();
s = null;
}
Window: EditOrderWindow
public partial class EditOrderWindow : Window
{
ViewModel vm;
AddItemWindow aiw;
public EditOrderWindow(ViewModel _vm)
{
InitializeComponent();
vm = _vm;
this.DataContext = vm;
vm.AddItem -= Vm_AddItem;
vm.AddItem += Vm_AddItem;
}
private void Vm_AddItem(object sender, EventArgs e)
{
if (aiw == null)
{
aiw = new AddItemWindow(vm);
}
aiw.ShowDialog();
aiw = null;
}
}
Window: AddItemWindow
public partial class AddItemWindow : Window
{
public AddItemWindow(ViewModel _vm)
{
InitializeComponent();
this.DataContext = _vm;
_vm.ItemAdded -= _vm_ItemAdded;
_vm.ItemAdded += _vm_ItemAdded;
}
private void _vm_ItemAdded(object sender, EventArgs e)
{
this.Close();
}
}
i'm coding in c#/wpf (i'm a beginner), i have 2 windows:
window1 contains a listview , window2 contains textboxes in which i retrieve their values to do some treatement to obtain in the end a list(collection that i created) in window2 and the problem is that :
i want to fill my listview (window1) from window2 with the iformations obtained from the collection (window2); but i can't
code window1
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Ajouter_Click(object sender, RoutedEventArgs e)
{
Window1 a = new Window1();
a.List_inf = new List<Listview_content>();
viewMed.ItemsSource = a.List_inf;
a.Show();
}
code window2
public partial class Window1 : Window
{
public List<Listview_content> list_inf;
public List<Listview_content> List_inf
{
get { return list_inf; }
set { list_inf = value; }
}
public Window1()
{
InitializeComponent();
}
private void valider_Click(object sender, RoutedEventArgs e)
{
Listview_content med_inf = new Listview_content();
med_inf.marque = "marque";
med_inf.restitue = true;
med_inf.quant = 40;
med_inf.prix = 14;
list_inf.Add(med_inf);
}
An easy solution :)
In Window2 create a list
public class window2
{
public List<string> ItemList = new List<string>;
public void btn_Add()
{
ItemList.Add("StringHere");
}
}
In Window1
public class window1
{
Window2 win2 = new Window2;
public void btn_Click()
{
win2.showDialog();
foreach (var item in win2.ItemList)
{
combobox1.Items.Add(item.tostring);
}
}
}
trying to get data from the main form to form 2. The main form has a textbox
and a button. when the button is pressed it opens form 2 which will display the data entered in the main form as a series of text blocks.
However I cant get the data to transfer between the forms. the code is bellow.
can anyone help or suggest anything I can do differently?
WPF 1 main form:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenForm_Click(object sender, RoutedEventArgs e)
{
//btnset: Takes the values contained in the text boxes and updates
//the student class
//properties.
Student.sFname = firstname.Text;
Student.sSname = secondname.Text;
Window1 details = new Window1();
details.Show();
}
WPF 2 code:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = Student.sFname;
Sname.Text = Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
There are a number of ways to "pass data" between 2 classes. The easiest way is to expose property or method on Window1 and just set the text you need passed. Another way is to create a constructor on Window1 that takes in the data as parameters. Here is code that demonstrates these approaches.
public class Program
{
public static void Main(string[] args)
{
var c1 = new Class1();
c1.DoStuff();
}
}
public class Class1
{
public void DoStuff()
{
var c = new Class2("stuff");
var c2 = new Class2();
c2.AcceptStuff("stuff2");
c.Print();
c2.Print();
c2.MyData = "stuff3";
c2.Print();
}
}
public class Class2
{
private string _myData;
public Class2()
{
}
public Class2(string myData)
{
_myData = myData;
}
public string MyData
{
set { _myData = value;}
}
public void AcceptStuff(string myData)
{
_myData = myData;
}
public void Print()
{
Console.WriteLine(_myData);
}
}
Prints
stuff
stuff2
stuff3
I assume you have a class in MainWindow like:
`Public class Student
{
public static string sFname;
public static string sSname;
}`
When you click open button you are assigning values to those variable, but if you want to access them in another window mention the window name and then class name.
Check this code if its working?
`public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void details_Load(object sender, EventArgs e)
{
Fname.Text = MainWindow.Student.sFname;
Sname.Text = Mainwindow.Student.sSname;
}
private void Close_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}`
I have a issue with passing information from one wpf window to another. For some reason when main window is loaded nothing is set in the label, I need to be able to keep the data in a string to use for anything (label not important but shows what I mean)?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public string MyData { get; set; }
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
label1.Content = MyData;
}
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string mytext = "blabla";
MainWindow fromloginwindow = new MainWindow();
fromloginwindow.Mydata = mytext;
}
Or am I doing this the wrong way round?
EDIT:
Please do not go on a tangent about the label its unimportant I need to be able to get and set a string for use anywhere in the MainWindow. Also the string "mytext" is also irrelevant as obviously I will not be setting the string this way.
It sounds like you are running into an event lifecycle issue; the calls to the Loaded event happen pretty quickly and thus, the chance to set the text has passed. Instead, what you should do is either:
1) Bind the Property to the Label in the XAML
public event PropertyChangedEventHandler PropertyChanged;
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
protected string _myData = string.Empty;
public string MyData
{
get { return _myData; }
set { _myData = value; NotifyPropertyChanged("MyData"); }
}
protected void NotifyPropertyChanged(string propName)
{
var methods = PropertyChanged;
if(methods != null)
methods(this, new PropertyChangedEventArgs(propName));
}
<Label Content="{Binding MyData}" />
2) Set the control text via another method (or inside the property declaration):
public void SetLabel(string text)
{
label1.Content = text;
}
protected void button2_Click(object sender, RoutedEventArgs e)
{
MainWindow x = new MainWindow();
x.SetLabel("blabla");
}
The Loaded event occurs before you set MyData, change the code like this:
public partial class MainWindow : Window
{
public MainWindow(string data)
{
MyData = data
InitializeComponent();
}
Have you tried passing the value to the second window through the window's constructor?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public MainWindow(string data)
: this()
{
label1.Content = data;
}
}
public partial class LoginWindow : Window
{
public LoginWindow()
{
InitializeComponent();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
string mytext = "blabla";
MainWindow fromloginwindow = new MainWindow(mytext);
}
}