How to change control template from the view model in WP7? - c#

Hi basically I've been able to completely seperate my logic into a view model with the exception of one thing. I have a a button and two different templates that can be applied to the button. Basically when audio is playing one template is displayed and when its not the other is displayed. When I was using the code behind this was easy because I had access to the page's Resources and all I had to do was set it accordingly. Now that my code is in the VM though I'm not really sure how to change the template.
The first thought that comes to mind is to maybe send a message to the view to change it when I need to. Is this the only way? I did see that WPF had triggers which looks pretty nice because you don't have to send messages and no code is placed in the code behind. Has anyone found a work around for this?

You can probably write a ValueConverter that would look at a public bool IsPlaying property and transform it to the appropriate template. Then you can databind your Button template property to the IsPlaying property, using the converter as a translator.
I haven't tried this with templates, but I've used it to great success with Bitmap and Visibility, so I'm pretty sure it would work here as well.
Here's a good intro to ValueConverter, if you haven't used one before.
That said, messaging isn't such a horrible solution in this case. Sometimes there is code that just wants to live in the codebehind file. Animation start/stop code is another example that comes to mind.

Related

How to make programmatic changes to views in the controller appear in the storyboard

I recently inherited a Xamarin project where I am to work on the iOS project. Going over several tutorial I figured I was ready, however the person before me did not use storyboards or controllers! They did all customization of views in files named LoginScreens.cs (basically controller files from the looks of it).
For learning purposes and ease of transition I would like to get a storyboard going in this project. So I created a storyboard titled Main and added a ViewController and essential copy and pasted the view customization code used in the LoginScreen.cs into my controller.
To be a little more specific, I have a Main.Storyboard that looks like this:
Where I am using editText boxes and a button to act as place holders for what I actually do to them in the ViewController.cs.
This all seems to register and builds properly however when I run the debugger on iPhone 6s iOS 9.3 I get the following:
The changes appear to show, but all my storyboard iOS designer views remain in place. I am trying to see if there is a way to reflect the changes made in the controller on the storyboard.
TL;DR: I'm trying to alter some views in a programmatic way in my ViewController.cs file. These views were originally added via the iOS designer and for customization purposes, they were edited in the controller. I want to see the visual alteration I make on a view in the controller, reflect in the iOS designer and when I debug.
Sounds like you are trying to go from a project where views were all done programmatically to implementing storyboards. This is a Big change and will take some time to convert the views over.
In the cs file there will be things like Add(passwordTextFeild) which are going to add more views to your storyboard view, hence why you get alot of views in your login view. You wont be able to see these in the storyboard as they are done at runtime.
If you are looking for IBDesignable this is more for custom controls and you still will have to add code to be able to see the changes from the .cs file in the storyboard.
Check my Question for IBInspectable/IBDesignable in Xamarin
Check this official link: https://developer.xamarin.com/guides/ios/user_interface/designer/ios_designable_controls_walkthrough/
Probably not what you wanted to hear but the UI in iOS projects tend to be done in one of the three methods:
All programmatically
Storyboards
Xibs
There are tons of questions/blog posts (even a video), weighting up the pros and cons of each. So possibly the last developer felt it was best to do it programmatically
There is a setting, IBDesignable, that you can add to the declaration of UI classes in Swift or Objective-C. That tells Xcode the those UI objects have a custom interface that you want to be presented in Interface Builder.
I have no idea how (or even if you could) you would use IBDesignable in Xamarin/C#. If its not supported for Xamarin then you're probably out of luck.
I suggest you search on "Xamarin IBDesignable" on the net.

More views of a form in c#

Let's say I have two "views". Each view has it's own button, which makes other view to appear. All should be managed in one window. So how do I achieve this? Im looking for something like viewController in iOS...
I tried to use one filled, docked panel - but than all classes are active, so it doesn't seem like a good solution. I also tried user classes (like this), it works, but it's complicated and I have big deal sending data between these classes.
There is no such thing as "views" of a form. The concept of what a UIViewController can do in iOS is different than building a properly functioning form in C#. You need to learn some new skills now and approach this from a different perspective.
The basic principle is to build a form with controls (either manually or through code or both), change the properties of those controls manually or through code and use the methods they support. You can do what you want, but it's going to take learning some new things.
Try checking this out:
https://msdn.microsoft.com/en-us/library/360kwx3z(v=vs.90).aspx
It's not 100% clear what you are trying to do, but it sounds like you should look into User Controls or Composite Controls.

Data Templating with user input fields

I'm trying to build a confirmation window that lists to the user all the changes he has made on a given object.
I have already built eh backend that is simply a Dictionary<string, IConfirmation> that lists confirmations that should be made.
However I don't know how to generate a template for each of them that would look like the mockup below, and load them all in a confirmation window after that.
I've already looked at the Data Templating from msdn but it seems to only template static data (i.e. no input possible neither embeded control).
Any idea? Thanks in advance for your help!
Don't expect for code since you didn't provide any code to start with :). But DataTemplate seems to be the right option for this, in my opinion. The above MSDN link you posted shows exactly how to use DataTemplate in detailed, step by step manner. You can just follow what explained there, and change TextBlocks to TextBoxes or any other input control you want to use. Using input controls shouldn't be an issue for DataTemplate as long as you use DataBinding in two way mode.

User control vs Control Template in WPF

I have recently reviewing someone's code and come across a User Control whose UI is like this screenshot
This control has no code in its code behind file, i am thinking we could move all the xaml code of this user control to a control template and use it whereever required. I want to know if this a good practice and should i do so?
Is their any performance and design benefit of one approach over another?
I want to know if this a good practice
So first we should define good practice? I will give the agile good practice point of view: do it only if you need it. Thus for you the answer is in your question:
use it whereever required
Is it required in any other place? If so, use a Control Template. If not, use a User Control, which will be more readable for the next developper anyway (Keep It Stupid and Simple).
Since ultimately this is going to be a UserCotrol,Now suppose some other team working on other module require this control and they want Loaded event of the Datagrid within this Control, Now if you create it as datagrid within usercontrol then they can easily access that datagrid and subscribe Load Event to it. If you will do it in ControlTemplate then it will be difficult if not impossible.

WPF - Have a list source defined at runtime but still have sample data for design time

I have some ListBoxes in my WPF app. I would like to be able to view how the design looks with out having to run the app.
But I still want to be able to bind to ItemsSource to my View Model.
I know I saw a blog post on how to do this, but I cannot seem to find it now.
To reiterate, I want dummy data at design time, but real data at run time and not break the MVVM pattern.
Any ideas?
You can check whether your code is in design mode or not. Here's a great post about doing this in different situation.
Detecting design time mode in WPF and Silverlight
Have you tried the sample data option in Blend 3? With a control open, look at the DATA tool window. There's a way to create sample data... think it does exactly what you're looking for:
http://silverzine.com/tutorials/how-to-create-sample-data-in-blend-3/

Categories