C# WPF radio buttons split onto different pages - c#

I'm working on WPF (XAML) using the designer (very newbie) and C# as backing code.
I need to be able to have a few "pages" of Radio Buttons in the same group (i.e. clicking on item A on page 1 then clicking on B on page 2 should deselect A on page 1).
How would I go about doing this?
Thank you!
Note that the "buttons" are actually just radio buttons above images.

First, I would NOT recommend doing this. As a user, I would be very confused/surprised if my selections on one page affected a selection on a previous one.
As to how to accomplish it, I will assume you are using MVVM (as you should be in WPF). First, the ViewModel for each page needs to have a reference to the same backing property in the Model. In other words, your Model has a property like (where MyRadioSelection is some enum you are binding to):
public MyRadioSelection GlobalSelection {get; set;}
and your view models have:
public MyRadioSelection UserSelection
{
get {return Model.GlobalSelection;}
set
{
Model.GlobalSelection = value;
OnPropertyChanged(UserSelection);
}
}
Then you just need a ValueEquals converter (from this answer), bind all your radio buttons to the enum, and you should be good to go!
Please let me know if you would like a piece explained further. Also, posting your existing code would help improve this answer. Also note that your Model may need some way of notifying the ViewModels of external (another ViewModel's) changes to the backing property. You could approach this from several directions, but just implementing INotifyPropertyChanged and listening to it in the ViewModel would work.

Related

Mvvm - How to capture in the ViewModel, the UI's data-bound Control's Name/Id, using a Parameter Command? Is there a clear cut way I'm missing?

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

A comment list Using WPF

I would like to know create this sort of comment list with WPF using using input from the user. He will input the username and a comment. Then he will press submit. There will also be a time stamp on every comment submitted. Then the system will have a limit of 5 comments once a different user inputs the 6th comment the first one will be deleted and then the 2nd comment submitted will become the first and so on until the users stop submitting.
Sadly I am completely stumped and just can not figure out how to even start it, so some help would be greatly appreciated.
I would personally go with something around this way:
ViewModel layer
You will need to create a UserComment class containing the following properies
UserName,Comment,TimeStamp
Another class which acts as the ViewModel of your UserCommentsView containing a property UserComments which returns an
ObserableCollection of UserComment type.
A ViewModel.AddUserCommentCommand property which adds a comment to
UserComments - but before it checks the count of items in
UserComments and removes the first/last (depending on your design)
before adding a new comment.
View layer
A listview with the above ViewModel as its context + items source
being ViewModel.UserComments.
A Button with its command bound to ViewModel.AddUserCommentCommand.
Have a look at Basic MVVM Listbox Binding in WPF for MVVM binding.
and if you prefer code-behind: ListView, data binding and ItemTemplate

Making a grid of objects

I'm trying to make something like a quiz application where 3 questions will be brought up on screen at a time, allowing the user the check a radio button containing "Yes" or "No", and have an answer come up appropriately to his response. The questions will continually come from a database I'm using.
After a few attempts I've figured that using x:Name property is not a great solution, since it doesn't allow me to use a loop in order to change the questions and answers. Is there any other way to make a grid with the same types of objects in each cell, being able to access each object inside each cell of the grid in the code-behind?
Here is list of steps you need to implement,
Need to create QuestionModel, contains question properties, make sure your model inherits INotifyPropertyChanged.
Need to create ViewModel, which contains data objects, public/dependency properties
Need to bind/set data objects/properties on viewmodel constructor
Need to set your ViewModel as a DataContext of your View(.xaml) (You can create this on zammel directly and codebehind as well
Need to bind your UI objects like Question/answers/yes-no with viewmodel properties accordingly
WPF/Silverlight has their own fundamentals like Data Binding, Resources, Compiler, Dependency Properties. Above steps comprises MVVM design pattern. During each steps, please google for specific stuff.

Highlight certain rows in Listbox

I have a listbox I'm setting with a datasource of highscores
public class HighScore
{
public string Username {get;set;}
public int Score{get;set}
}
var IList<HighScore> HighScores = getAllTheScores();
MyListbox.ItemsSource = HighScores;
I want to change the background color of any rows which have a Username property equal to the currently logged in user (stored in AppSettings). I've seen Converters but this would need to somehow get hold of the currently logged in user which doesn't seem like something a converter should be responsible for getting.
I could also iterate the listbox items but from what I've seen that's not advised and I should be preferring binding to code behind drilling into controls.
Any suggestions as to how best achieve this much appreciated :)
Two suggestions. You already mentioned the first one, which is to use a ValueConverter. If value value is accessible from a viewmodel (recommended) than that is easy to pass along to the ValueConverter. If it is not stored in a view model then there are no worries with getting this from within the value converter provided it's named properly.
The second suggestion is to use a Behavior. The Behavior would be responsible for setting the background based on the user. I don't see much advantage here over a value converter except for the back that it's not a value converter. It would (almost) do the same thing. Overall I would recommend a value converter. Easy to implement, low code overhead and little xaml needed.

A viewmodel's role beyond databinding?

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.

Categories

Resources