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.
Related
Mvvm, Wpf, VS2015
Hi All! I have googled this till my fingers bleed! But just hacks and work-arounds, or partial use of code behind.
My Wpf-Mvvm app uses Speech Synthesis.
Each button, (and in the next iteration, buttons using the selectedItem Index of Lists or ComboBoxes, to select the content(text)to be synthesized) specifies different content to be synthesized.
Sticking with Mvvm principles, in my viewModel, I need to capture which button, based on the List or ComboBoxes' SelectedItem/Value that is bound to the Parameter Command.
Why? So I can have the app synthesize the content(text/words)the user selects to hear. If not, I have to make separate commands and speech synthesizer methods for each.
There must be a simple clear-cut way. What am I missing here?
I thought of updating the Model's properties to reflect user's selection and use User Settings to persist,but have not tried yet.
Otherwise the Model's properties are accessible so far as encapsulation-wise to the ViewModel, but the property values from the Model still are not visible in viewModel.
I need the command bound control's ID/Name to route the content to be synthesized based on the model's class properties. But when I instantiate the model from the viewModel,
the model's properties are not there. I presume instantiating the Model class object from the viewModel, is in a different memory location, than the Model.
So I will try using User Setting's persistence for these properties, if no one else has a better way.
There has to be a clear solution to this, I hope :)
I know the View and ViewModel are supposed to not need to know what the other is doing. In this case though, I clearly need the List or ComboBoxes' Id or Name and their SelectedItem index.
I also need the button's Id or Name, because I have several groupings of content to choose from, and several buttons starting the synthesizing method.
Example: If the text content of an announcement to be synthesized, held in separate XML(SSML) files with identifier key values of 941, 23, 508, etc.,
I need to know which the User Selected to hear.
I imagine simply capturing in the viewModel, the Data-bound List/ComboBoxes' Selected ID index would suffice.
Thank you so very much for your help.
-Flazz
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
I've a table in a database where I need to show all the rows's content (as a TextBlock) and to the right of each TextBlock I need to show a TextBox so the user can enter a value (a number) for each row and also I need to be able to change the color of any TextBox when the value provided by the user is negative.
Can someone give me a clue with this?
PD: I'm using WPF with Prism 4 and MVVM pattern and VS2010 Ultimate
I won't give you a complete solution, but I can point you in the right direction.
I start by creating a data structure that contains properties for Name and Value, and that implements INotifyPropertyChanged for Property Change notification.
Next in the ViewModel (or possibly Model), I would make would be an ObservableCollection<MyDataObject>, and populate it with the data from the database.
In the XAML, I would use an ItemsControl bound to the collection, and overwrite the ItemTemplate to render each item as either a Horizontal StackPanel or a Grid, containing the Label and TextBox
For the TextBox.Foreground property, I would bind it to the same value that TextBox.Text is bound to, except I'd also use a IValueConverter in the binding which checks to see if the value is above or below 0, and returns the correct color. Since it is a binding, it will automatically update whenever the value changes.
<TextBox Text="{Binding Value}"
Foreground="{Binding Value, Converter={StaticResource MyCustomConverter}}" />
It'd be nice to know what you've tried so we can help you better, but you almost certainly want to be binding your data to a DataGrid or, if you absolutely need more flexibility (so far it doesn't sound like that's the case), an ItemsControl. You don't want to be just creating a Grid dynamically.
Your question about the TextBox and how to change it's color when the value is negative is actually a separate question from how to do your layout. I'd look into the Validation components of WPF for that.
i am writing an application that has a viewmodel and a usercontrol that displays
data from this viewmodel. The viewmodel contains an entity "Appointment", and those
appointments have a property "UserName".
When I display the appointments, I want to use a value-converter to get a color for
the user (depending on "UserName"), but the colors are not contained in the entity "Appointment", so I wanted to create a value-converter that uses the entity "User" from the viewmodel.
What is the best way to use another entity from the viewmodel inside the converter?
Is it possible to access the viewmodel from the usercontrol? I tried to place the converter inside my viewmodel-class, but can I access this class from the usercontrol?
I figured out that the following possibilities might work:
Adjust the viewmodel so that each appointment also contains the color. But I don't want to do this because I don't want to mess with the viewmodel.
Set the converter-parameter from the class that also contains the viewmodel at startup. (Does this work?)
Use x:Reference to databind the converter parameter to the viewmodel that is unknown at compile-time.(Is this possible?)
Converter parameter is the way to go.
Why is the viewmodel unknown at compile time?
Bindings are not compile time checked anyway.
Is the UserControl.DataContext being set to an instance of Appointment, you should be able to set the parameter to {Binding UserName} or {Binding Appointment.UserName} depending on exactly what you are setting as the DataContext on the UserControl.
I would suggest that you re-examine your reluctance to modify the view model. The purpose of having a view model in the first place is so that everything that the view needs can be found in one place. Coming up with elaborate value converters to prevent modifying the view model is an approach that gets increasingly unmaintainable the more you do it.
I'm a bit confused as to what a viewmodel's role is beyond databinding. I have a menu built in silverlight. The menu has x number of menu items which is determined at runtime. One of the features I would like to add to this is that each menuitem has a different text colour when hovered over.
Is it the role of the view to have a colour selector method or should the view handle this in it's code behind?
Normally I would keep the coloring/styling in XAML if possible - My view of the ViewModel is that it is responsible for providing all the data (ie. not graphical stuff) from the Model in a manner the View can consume.
If it was complex logic that determined the color and it was to be reused - I might be tempted to put it in the ViewModel tho.
The view model is used by the data binding process as a "safe" way to allow you to sort/filter/group the records as seen by a specific control without (necessarily) making changes to the actual bound data set (that is, unless/until you tell it to). (FMI read Bea's article here.)
I agree with Goblin here, in that the presentation aspects like color might be best kept separate in the XAML, for example in the DataTemplate used by that control.