How to have separate pages databound? - silverlight

I'm wanting to use a datasource to go through records but one at a time per page.
So I'd like a whole page to be dedicated to a single record.
How would I do that?

Easiest way is just swap out the DataContext on the page. So don't bind to the list, bind to individual items in list.
private List<Question> _questions = new List<Question>();
private int _currentItem = 0;
private void nextButton_Click(object sender, RoutedEventArgs e)
{
_currentItem++;
this.DataContext = _questions[_currentItem];
}
private void backButton_Click(object sender, RoutedEventArgs e)
{
_currentItem--;
this.DataContext = _questions[_currentItem];
}
Now obviously you would have to add bounds checking to make sure you don't go past the last question, or before the first question, but you get the idea.
If you are using MVVM the idea is pretty much the same - you want the viewmodel to model a single item rather than the list of items, then you just swap the data for that current item as you navigate forward or backwards.

Related

Winform Timer isn't running if statement when criteria is met

Hi I've come across either a weird bug or I'm not understanding something.
To cut a long story short I've had everything I'm wanting to work on my form working fine, I then decided to turn the form into an options menu so it was no longer the first form that appears when the application is launched and is shown after I click a button on a different form with the code
private void ShowOptionsButton_Click(object sender, EventArgs e)
{
formHomePage.Show();
}
And for some reason a timer if statement is no longer working:
private void StartSubCheckT_Tick(object sender, EventArgs e)
{
if (subliminalMessages.Count > 0)
{
MessageBox.Show("list greater than 0 if");
StartSubB.Enabled = true;
}
there are other if statements below but are irrelevant and the point of this is to make a button usable once a list is greater than 0. I've created another test button to display the value and it shows that the sublminalMessages list is greater than 0
private void testbutton3_Click(object sender, EventArgs e)
{
MessageBox.Show(subliminalMessages.Count.ToString());
}
Which outputs at 1 which it should be from some other code that adds a value in at the beginning. But for some reason even with the subliminalmessages.count being greater than 0 the if statement is no longer being called ever since I'm making the form appear being called from another form from the button code above.
The subliminalMessages list is being populated and created on the same form
public List<string> subliminalMessages = new List<string>();
private void Form1_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(Settings.Default["Subliminal1"].ToString()))
{
subliminalMessages.Add(Settings.Default["Subliminal1"].ToString());
MessageBox.Show("If worked");
}
}
There is a value in the Setting.Default that is being added
The button and timer are on the same form and the timer in question is enabled.
Does anyone know why?
Thanks
I'll have a stab at giving you an answer. But it's a little swerve from what you're doing now.
From what I understand of your code you are using the timer to enable/disable the StartSubB button. Or maybe just enable it.
Instead of relying on the timer which appears to not work why not use a BindingList<string>. This has an event called ListChanged which you can handle and then enable/disable your button.
Here's a test form I created:
public partial class Form1 : Form
{
BindingList<string> items;
public Form1()
{
InitializeComponent();
button3.Enabled = false;
items = new BindingList<string>();
items.ListChanged += Items_ListChanged;
}
private void Items_ListChanged(object sender, ListChangedEventArgs e)
{
button3.Enabled = items.Count > 0;
}
private void btnAdd_Click(object sender, EventArgs e)
{
items.Add("a");
}
private void btnRemove_Click(object sender, EventArgs e)
{
if (items.Count > 0)
items.RemoveAt(items.Count - 1);
}
}
I have a BindingList<string> called items. This is analagous with your subliminalmessages list but it's a BindingList. This type of list has a ListChanged event that fires when items are added or removed from the list. In the constructor we new up the items list and subscribe to the ListChanged event.
When the Items_ListChanged event fires button3 is enabled or disabled based on whether items.Count > 0 or not.
In btnAdd_Click we just add an item to the list.
In btnRemove_Click we check that there are some items then remove the last one.
If you were to run this you'd see that when we click the Add button, button3 gets enabled. If we click the Remove button we'll see button3 get disabled.
The only limitation of BindingList is that you can't add a range by passing in another List.
If you implement this and your button still doesn't activate then you'll probably need to post some more code. Strip out all the irrelevant stuff and put it in a new project that demonstrates the failure of the condition and either copy the code here or provide a link to download the project. The only reason the if statement should fail is if the list is actually empty.

Data binding across several windows with WPF / Entity Framework? Pass context around or not?

Most WPF/EF tutorials only cover databinding in one window. However, in reality data gets displayed across many windows. You often display a record in the first window and dig deeper in related details in the next windows.
So, this also is the case in my scenario. Here you can see my data structure and the ui. Actually I am not dealing with Customers and Invoices, but the structure is the same. (My concrete questions are at the very end.)
In the InvoicesWindow I can select an Invoice and press "Show Invoice". That opens a CustomerWindow displaying Customer details and his invoices. The right invoice is pre-selected. To each Invoice displayed in the CustomerWindow I can add Items or edit them. This is done in a seperated window called "ItemWindow". Editing the DataGrids is not an option. They are set to ReadOnly.
Here is the code of the wpf-window classes (I only have done displaying data yet, not saving):
Invoices Window:
public partial class InvoicesWindow : Window
{
private MyEntities context = new MyEntities();
public InvoicesWindow ()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
CollectionViewSource invoicesViewSource = (CollectionViewSource)FindResource("invoicesViewSource");
invoicesViewSource.Source = context.Invoices;
}
private void ShowInvoice_Click(object sender, RoutedEventArgs e)
{
Invoice selectedInvoice = (Invoice)InvoicesDataGrid.SelectedItem;
var customerWindow = new CustomerWindow(selectedInvoice);
customerWindow.ShowDialog();
}
}
Customer Window:
public partial class CustomerWindow : Window
{
private MyEntities context = new MyEntities();
private Invoice selectedInvoice;
public CustomerWindow()
{
InitializeComponent();
}
public CustomerWindow (Invoice selectedInvoice)
{
InitializeComponent();
this.selectedInvoice = selectedInvoice;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Set the data
CollectionViewSource customerViewSource = (CollectionViewSource)FindResource("customerViewSource ");
customerViewSource.Source = context.Customers.Where(p => p.id == selectedInvoice.Customer.id);
//Select the right invoice
CollectionViewSource customerInvoicesViewSource = (CollectionViewSource)FindResource("customerInvoicesViewSource ");
customerInvoicesViewSource.Items.MoveCurrentTo(((ObjectSet<Invoice>)customerInvoicesViewSource.Source).Where(p => p.id == selectedInvoice.id).SingleOrDefault());
}
private void EditItem_Click(object sender, RoutedEventArgs e)
{
Item selectedItem = (Item)ItemsDataGrid.SelectedItem;
var itemWindow = new ItemWindow((IQueryable<Customer>)(customerViewSource.Source),selectedInvoice,selectedItem);
itemWindow.ShowDialog();
}
}
Item window:
public partial class ItemWindow : Window
{
private Invoice _selectedInvoice;
private Invoice _selectedItem;
private IQueryable<Customer> _customers;
public ItemWindo()
{
InitializeComponent();
}
public ItemWindow(IQueryable<Customer> customers, Invoice selectedInvoice, Item selectedItem)
{
InitializeComponent();
this._customers = customers;
this._selectedInvoice = selectedInvoice;
this._selectedItem = selectedItem;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Set the data
CollectionViewSource customerViewSource = (CollectionViewSource)FindResource("customerViewSource");
invoicesViewSource.Source = _customers;
//Select the right invoice
CollectionViewSource customerInvoicesViewSource = (CollectionViewSource)FindResource("customerInvoicesViewSource ");
customerInvoicesViewSource.Items.MoveCurrentTo(_selectedInvoice);
//Select the right item
CollectionViewSource customerInvoicesItemsViewSource = (CollectionViewSource)FindResource("customerInvoicesItems");
customerInvoicesItems.Items.MoveCurrentTo(_selectedItem);
}
}
I wrote the code out of my mind. So, maybe some casts are missing and some methods are mis-spelled. I hope I got the right type with "ObjectSet", it also could be "ObjectCollection" or something like that.
The XAML was created widely with assistance of VS2010 like in this video: http://msdn.microsoft.com/de-de/data/ff806174.aspx
So, finally my questions ;)
Is the design of binding I applied correct?
In CustomerWindow I create a new context.
Between CustomerWindow and ItemWindow I just pass the data of the same context and select the current item manually.
In CustomerWindow I use a ObjectSet (or ObjectCollection, I'm not sure about the type anymore) with a single entry as Source for the customersCollectionViewSource. This works fine. However, there is no need for a collection, because I only edit a single Customer. I did not manage to set a single Customer as Source. I didn't know how to adjust the view source which was generated by VS2010.
I haven't done saving yet. But I think I am going to run into problems due to my design between CustomerWindow and ItemWindow. Maybe you can give me some advice here.
When the "Apply"-Button in ItemWindow gets pressed, the Item data should be updated in DB. But not the Customer- and Invoices-related data in the CustomerWindow underneath.
The DataGrid of Items in CustomerWindow should get updated, when closing the ItemWindow. But not the rest of the fields in the CustomerWindow, since here could have been data changed before opening the ItemWindow.
The only solution for me to overcome that "synchronisation problem": The User is forced to press "Apply" in the CustomerWindow before he can press the "New Item" or "Edit Item", if there have been any changes. (Kinda like the "window resolution control" of windows 7 when working with two monitors) But this is not too user friendly.
A cleaner design would be to use the MVVM design pattern.
Inject the view model into the window's context and bind the view model to either a collection of entities or a single entity, bind in the xaml to properties in the view model(s) and use commands implemented in the view model for actions e.g. add new, delete.
The windows shouldn't be aware of the context.
If you have a list view model + window and a details window (preferably with a view model), then the list view model should pass the selected item to the details view model (or window) as the context.
If the windows are not open at the same time or do not have related objects, then their views models should not share a database context, otherwise, in order for the changes to be reflected easily between the windows, they will have to share the database context.

how to update DataGrid if data goes through pipe?

I have such code:
private void Window_Loaded(object sender, RoutedEventArgs e) {
........
List<ConsoleData> datas = new List<ConsoleData>();
foreach (StrategyDescriptor sd in pipeProxy.GetStrategies())
{
datas.Add(pipeProxy.GetData(sd.Id));
}
dataGrid1.ItemsSource = datas;
}
the problem is that dataGrid is never updated.
I would like to update it every second or so.
I'm very novice to WPF so sorry about this question - how to do that? Should I create timer or there are better technics?
You need to use an ObservableCollection rather than a List if you want the UI to be aware of changes. And the classes in your collection must implement INotifyPropertyChanged.

Winforms, Combobox, Databinding... allow user to type in a value not found in the DataSource

Just like the title says... I have a Winforms application with a databound dropdown. I want the user to have the convenience to pick from a bunch of predefined values, but also the ability to type in his own value
If I just enable databinding and set dropdown type to anything but DropDownList, it allows me to enter anything I want, but does not persist it to the objects...
Seems like a simple problem to solve... help?
I've added an event handler on ComboBox.Leave this code would add the newly typed in string in the combobox to the underlying list(countries) as well as refresh the combobox binding to it.
Limitations
You'd have to handle the addition of new element based on the type of datasource you have.
The List.Contains is case sensitive you might want to keep all the strings in one case. And convert the user entered value to that case before deciding to add it to the datasource.
Here you go, modify the comboBox1_Leave eventhandler according to your datatypes and datasource.
public partial class Form1 : Form
{
private List<string> countries;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
countries = new List<string>();
countries.Add("Australia");
countries.Add("Belgium");
countries.Add("Canada");
comboBox1.DataSource = countries;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
ComboBox combo = (sender as ComboBox);
CurrencyManager cm = (combo.BindingContext[combo.DataSource] as CurrencyManager);
if (!cm.List.Contains(combo.Text))
{
cm.List.Add(combo.Text);
cm.EndCurrentEdit();
cm.Refresh();
cm.Position = cm.Count - 1;
}
}
}

Retrieve treeview item

In my treeview I have text. After I select that, I want to retrieve that selected item as string and I need to pass this string to various functions.
I don't know how to get the selected item.I coded like
private void treeview1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeViewItem selectedTVI = null;
if (treeview1.SelectedItem != null)
{
selectedTVI = treeview1.Tag as TreeViewItem;
}
}
But selectedTVI shows NULL.What can I do?
TreeViews display lists of items, not lists of TreeViewItems.
TreeViewItem.SelectedItem is the element that is selected, if your tree view has a collection of Car objects that it is displaying, the SelectedItem will be of type Car.
try this
private void treeview1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (treeview1.SelectedItem != null)
{
Console.WriteLine(treeview1.SelectedItem.ToString());
}
}
im pretty sure the SelectedItem is the object you are looking for.
(by the way your 20% acceptance rate sucks a little - and is probably one of the reasons you dont get your questions answered quicker, if your question is answered, mark it as answered. This helps the whole community)

Resources