WPF design & binding - c#

I have a WPF application. I have a custom object that contain a list. The list is a custom type of "Order", the list contains 24 items. The list contains three properties. First property "baseCurrency" (i.e. GBP) of type string, second property "orderCurrency" (i.e. JPY) of type string & the third property "calculate" of type boolean.
In my WPF application I have a grid that is split into 24 equal size squares. In each square I want a control that contains a textbox and two small icon images. I want the textbox to be bound to the two string properties for an item from my list. The two small icon images are flags. So if we have GBP & JPY the two images would be their respective flags. I also want to bind the images based on the string provided. So if GBP is selects the gbp.jpn image in my project folder - not sure how I do this?
I'm think of using a toggle button as my control that will contain the textbox and images. I hoping to bind each button to a list item. The calculate property will be bound to the toggle button IsChecked property. If IsChecked is true I want my calculate property to be true.
Is there a better control that I should be using rather than a toggle button? Is there also a better way of trying to accomplish what I'm after?

Your situation is so common that the MVVM pattern is widely used in WPF for that purposes. To get started, you may read about:
MVVM - To build your application structure
Data Bindings in WPF - To bind your data to grid in a right way
Data templates for controls in WPF - To adjust the data bound
There is also a post on my blog related to working with currency in WPF application

Related

ViewModel or ValueConverter for WPF View

I have a WPF application to display incoming bytes from a serial stream. I want to display these bytes inside a user control that allows for changes (i.e. byte value changes meaning background color changes to alert user, user chooses to view data as hex/decimal/binary). So far, I have an
ObservableCollection<ByteDisplay>
where ByteDisplay is a WPF User Control bound to a data model with a few relevant properties: DisplayFormat (enum Hex/Binary/Decimal), Data (the actual byte value), and BgColor (a string representing a system color to denote that a value has changed).
My problem is that I have to completely replace the DataContext of the ByteDisplay to get changes to show in the aggregate view that holds this collection. I think I could get around this with a ValueConverter for the collection of raw bytes, instead of completing the change in the Aggregate View's ViewModel, but is this the right approach?
Why do you want to use an ObservableCollection? Sounds really strange though.
In my opinion, you can simply create a user control bound to a view model by following the MVVM pattern.
Here you can have textblocks, textboxes, datagrids, charts, etc which binds to properties in the view model. You could also bind the background property to viewmodel or use a converter as you said. Like if you have some other property referring to that, say you're displaying some value in a textbox and you use the same value for changing the background color to warn the user, then use a converter.
It's as simple as this. I did not understand the logic behind creating an ObservableCollection still.

C# ListView items with custom controls

I'm maintaining a C# application written with windows forms. I now need to have a list view where every item has a few custom controls.
Every item need to have title and a combobox. The problem is that the data for the custom boxes will be different. So for example Item 1 could have a ComboBox where you can pick 1-3. Item 2 would have a combo box where you can pick 1-2.
So in the property column I need a string, and in the value column a combobox, with different data sets for different items (or at least for different kinds of items)
I've been on this problem for a while, and I don't really know where to go from here.
Why don't you use a Property Grid control? It is composed by two columns, the left one being a fixed text for the key/title and the right one is dynamic, in the sense you can have comboboxes, textboxes, color selection controls, etc. for the value the user can input/select.

class concept in WPF

I'm new in WPF and I need to group many components in one element and make and add new instance of that element in window for each student in database like 2 textblock plus 1 textbox for each student, how can i do something like that?
This is where WPF really shines - you can use an ItemTemplate or a DataTemplate to style the UI with the underlying data objects knowing absolutely nothing about how they are being presented.
Check out Data Templating for an introduction. Effectively an ItemTemplate is a template (definition) of how each item should be rendered. A DataTemplate goes a step further and gives you the ability to select which template to use based on the data item being bound to, so you can have a list containing different types of objects yet still show them all in the same list/repeater control on the screen.

Different controls for editing and viewing data

I want the controls in my wpf window to change depending on whether the user is viewing or editing/inserting data. For example, I want to display a label at view time, but a text box (or combo box etc) at edit/insert time.
Can I do this with DataTemplates or do I have to have two controls for each data item and change visibility depending on what mode the form is in?
If DataTemplates will do the job, can anybody point me to some examples?
many thanks
mcalex
In my opinion, the better way to do this is using differrent data templates for control, in case when you always need to use them together.
In any case you have to select switch templates or controls mechanism. You can use DataTrigger (first answer) or ValueConverter

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.

Categories

Resources