Multiple Windows Navigating the same datacollection - c#

I have a (non MVVM) WPF C# application that evaluates oil and gas wells that I want to refactor to MVVM. One window shows the tabular information and another window shows the graphical rendition of the same data. Both have navigation buttons to move from well to well.
How can I have record selection changes in one window automatically change the selected record and datagrid focus in the other window using MVVM without having the ViewModels know the properties of the windows. Currently Window One shares the listview selected item property of the other Window.

You want Mode=TwoWay on your binding paths, with an INotifyPropertyChanged event on your objects. This should update every element bound to the data collection.
Check out http://msdn.microsoft.com/en-us/library/system.windows.data.bindingmode.aspx for more information on the Binding Modes as well as implementing INotifyPropertyChanged events.

Related

What events are raised when a control is selected in the WPF design surface?

I'm trying to build a WPF control inheriting from Selector and I want to change the state of the control when one of its items are selected in the design surface. Despite hooking on a debugger, I can't seem to capture any useful events from the control. I've managed to capture it being initialised and the wrappers for its child elements being initialised around the items of the selection, but I can't see any change of state, or event being triggered when the control is selected.
Importantly, I want to detect when the item is selected either by the user selecting the control in the design view or in the markup.
I'm trying to achieve a behaviour like that of the TabControl, where selecting a tab brings it to the front and exposes any content associated with the tab. I can't fathom how it works at design-time though.
What do I need to do to detect that a control is selected?

Refresh ItemsSource from other window

I have a application which has a parent window with some menus on it ,when the menu clicks corresponding usercontrol is loaded inside the parent window as child.The parent window supports many usercontrols on each menu click ,the usercontrols are docked into the parent window. I have a usercontrol namely Items master from which the user can add items and save into database, I have another usercontrol which has a datagrid with a Combobox column for selectiong Items .The both user controls are docked on the parent window .When I add an Item into the Item master but that product is not available on the datagrid .How to refresh the items source of the datagrid combobox column when a new product is added ?
The quick answer is: use something like an event-aggregator to pass messages around the system when data is altered, so that each individual screen can re-load their respective data. The more important part is in designing and structuring you application to allow views to access the common data effectively. Look into something like MVVM for UI design.

Updating a control from another control child

I have some problems trying to update components of surface elements. I dont know if my approach to the problem is wrong, since I'm new to the topic.
My point is represented by the following diagram
According to the option that is selected in the menu, load different user controls as only child of StackPanel but i'm habing problems for update the Listview from loaded user controls, example: when I save a new item I need to recharge the list of items in the ListView
MVVM would be a good pattern here. If you have a problem passing data between controls - why not introduce them on top of unified data layer? Consider this:
Three radio buttons in your Menu, each one's IsChecked property bound to Visibility property of your respective UserControl.
StackPanel holding all three UserControls
ListView bound to ViewModel's List<Item>
Each of your UserControls bound to ListView.SelectedItem: one of them using TextBlock for read-only, one using TextBox for editing. Third one would create new item in your List<Item>. You would have to create ItemTemplate for each or create one UserControl (since they look very much alike) and use DataTemplateSelector.
If you're not familiar with MVVM here is a good start. You can also use one of the existing frameworks like MVVM Light
You can create an event on your child
public delegate void HandleNAMEOFYOURHANDLEEVENT();
on your child class
public event HandleNAMEOFYOURHANDLEEVENT yourInstance;
to use it on your child class
if (!ReferenceEquals(yourInstance, null))
{
yourInstance();
}
and you declare it on your parent like other event.

creating a custom user interface in WPF

I have a SQL database holding a number of numeric and text values that get updated regularly. The exact number/type/names of these data points can change depending on the source of the database writes.
I would like to create a user interface editor, where the user can add database points to the UI and arrange them and format them as they want. If a new point is added to the database they can right click on the UI and say "add this point" and choose from a list of database points.
I'm looking for some pointers on where to start on creating this editor application, could something clever be done using XAML to dynamically create std WPF controls at runtime?
Doug,
Apologies, by database points I simply mean rows in the database that represent an item to be displayed in the ui.
Ray / Sushant,
Thanks for taking the time to answer, I'll have a go at both these approaches.
Si
Here is an easy way to do this:
Create a DataPoint class, including "Name", "Type" and "Value" fields
Create a DataPointView UserControl that exposes a Name property and a read-only DataPoint property. When the Name property is set, load the DataPoint from the database. Optionally use a timer to periodically reload the DataPoint (or subscribe to update notifications from your database).
Create a UIEditor class deriving from Window that exposes a CurrentForm property that is initially a blank Canvas
Add handlers for ApplicationCommands.Open, ApplicationCommands.Save, etc to use XamlParser and XamlWriter to load/save the layout from/to a file on disk (or a database)
In your UIEditor XAML, include a ContentPresenter bound to the CurrentForm property to hold the UI being edited. Also any other controls desired (Save button, Open button, palette, etc).
In your DataPointView's XAML, display the data point's name and value.
In your UIEditor class subscribe to mouse preview events OnPreviewLeftButtonDown, etc. Whenever a mouse move event follows a mouse down event on a DataPointView, capture the mouse and begin adjusting the DataPointView's Left and Top coordintates - this allows the user to drag the DataPointView around the Canvas.
In your DataPointView's XAML, include a ContextMenu whose ItemsSource is bound to "{Binding AvailablePoints, RelativeSource={RelativeSource FindAncestor,my:UIEditor,1}}", and make sure the AvailablePoints property of your UIEditor class returns a list of MenuItems with names of available data points and appropriate command an command parameter.
In your handler for the command bound in the context menu, add a new DataPointView to your CurrentForm Canvas and set its Name from the name given in the CommandParameter
Set Focusable=true on the DataPointView objects, and handle ApplicationCommands.Delete by deleting the focused DataPointView.
With this code written:
You can allow your users to edit your UI by showing a UIEditor window.
You can display your UI without the editing features by simply loading it from disk using Application.LoadComponent and displaying it in a window.
use WPF DataGrid available as WPF ToolKit in .NET 3.5 or it is standard for .NET 4.0. It has the following features:
Highly customizable Data columns
Use Editable Rows that can be persisted
Columns can be added/deleted/rearranged on the fly.
Can directly load column names from database
and a lot more.
I think that would be perfect.
Please mark the answer if it seems helpful. Thanks.

WPF: Weird data-binding issue when using same data source on two controls

So i have a combo box on the main window of my WPF app. I bind a List accessed through a singleton to the ItemSource of the combo box. All is fine. In a child window that the user can open, i have a ListBox bound to the same List in the singleton.
The Problem: when i change the selection of the list box in the child window, i can see the selection change for the combo box on the main window at the same time.
What is causing these two very separate controls to behave like there is some kind of synchronization between them? Is it an issue with binding both controls to the same data object?
If both of your controls have "IsSynchronizedWithCurrentItem" enabled, you could see this issue.
You can find out more about this property here.
You can bind both controls to the same object without having them synchronized, just mark the "IsSynchronizedWithCurrentItem" property as false, and you should be good to go.

Categories

Resources