Using C# instead of XAML in Silverlight - c#

Is XAML really necessary? If someone really wanted to, could everything displayed on a Silverlight page be declared at runtime in the C# code?
VISIFire has example code where they set up one of their classes, chart, in the C# code and then simply add it to the user interface with a simple C# call:
LayoutRoot.Children.Add(chart);
How would I do a similar creation with a class I already have defined in the XAML file but because of other reasons I need to declare it at runtime? If I could use it as it is in XAML I think it would work because there are no errors with its display. It is declared like this:
<views:PrescriberPage01 x:Name="DashboardPrescriberPage01"
Visibility="Collapsed" SelectedProduct="{Binding SelectedProduct,
Mode=TwoWay}"Grid.Row ="0"/>
How would I declare that in C# code?

Is XAML really necessary?
I suppose no, you can probably do everything that you can do in XAML in code. But XAML is really, really a much, much more convenient way to go. And I think few would dissagree with this.
How would I declare that in C# code?
You can create bindings in code. Example from MSDN. In your situation it would be something like:
PrescriberPage01 page = new PrescriberPage01();
page.Visibility = Visibility.Collapsed;
Binding myBinding = new Binding("SelectedProduct");
myBinding.Mode = BindingMode.TwoWay;
myBinding.Source = this; // or whatever object carrying the property
// "SelectedProduct" that you bind against.
page.SetBinding(PrescriberPage01.SelectedProduct, myBinding);
LayoutRoot.Children.Add(page);

XAML is preferable to describe user interface, in declarative way and I recommend use XAML without code behind and also I would recommend look at MVVM pattern

I would rather say "Is C# really necessary for GUIs when I can use XAML" ? XAML is really good and was explicitly design for GUI.
You have two options here. Load your XAML dynamicly.
Or create it in code. (other posted the solution while i was typing AGAIN! ;) )

Simple controls are easy but... you might also want some styling done to those objects +
you might want an animation or two... this is already(mostly) provided in xaml ... and it is uber easy... If u hate xaml so much you might want to think about "Blend";p

One specific example of something you can't create without Xaml is a DataTemplate.
You can write code which will create a DataTemplate from a XAML string, but you can't add children to a newed up DataTemplate directly in c#.

Related

Binding BarBackgroundColorProperty in Xamarin.Forms

I'm relatively new to C# and the idea of data binding (that is, I'm not sure I completely understand it). I am looking to try to bind NavigationPage's BarBackgroundColorProperty for sake of being able to change my root page's bar background color multiple times for a theme feature I'm trying to implement. I've searched around for examples of binding this property and haven't had much luck. Could someone point me in the right direction? If anything is unclear, please let me know. Thanks guys!
Assuming you have an instance of NavigationPage called myBindingPage and a view model that extends INotifyPropertyChanged called myViewModel that has a property called BarBgColor, you should be able to set up a binding like this:
myBindingPage.SetBinding(NavigationPage.BarBackgroundColorProperty, new Binding("BarBgColor"));
N.B. I haven't actually tried this, but the API doc does list BarBackgroundColor as a bindable property.

WPF Dynamically format text in a textblock

I feel like I'm going to reinvent wheel so I would like to know if WPF has bult-in support for what I'm trying to achieve. I'm building an app that will allow people to enter some text in a textbox, and then see it formatted in a textblock.
I would like that the user be able to format the text himself by inputing things such as
This [BusinessSpecificStyle] is [/BusinessSpecificStyle] a sample text
My purpose is to be able to easily change the presentation of all my documents by simply changing the underlaying rules in BusinessSpecificStyle. However I don't know what is the best way to implement that with WPF. I was thinking of using a BBCode parser like this one but supposing I go that way, I don't see how I will be able to convert the resulting XAML into TextBlock children programatically, and I seriously wonder if there isn't some kind of built in support for that.
Thanks for your help
IValueConverter is what you are looking for.
Create the converter and format your text based on the bindings passed from the XAML.
You can get multiple samples over the net for creating IValueConverter. Refer to the link here and here to get you started.
Not sure if you are asking for Converter here. To me it reads that you want to control the style of a block of text depending on some background and common style?
If that is the case, you want to set the inlines of your text block to seperate your text into run elements, which can reference a specific style resource.
<TextBlock>
<TextBlock.Inlines>
<Run>This</Run>
<Run Foreground="{StaticResource BusinessSpecificStyleForeground}">is</Run>
<Run>a sample text</Run>
...
in this case, you create a resource which defines the binding styles for run or bind the Style in it's entirety.
Apologies if I am making up a new question, I see you've marked an answer but wanted to add this just in case.

How can i access from code behind, controls that are in a stack panel?

This is in C# in WPF:
I know I can add items to a stack panel like so: myStackPanel.Children.Add(new Button());
Or to a ListBox like so: myListBox.Items.Add(new Button());
Of course, I could edit beforehand the controls and add them latter, like set the proprieties first then add them.
But how do I select the control once it is in the stack layout with code behind. For example, is there a way similar to this: myStackPanel.Childern.CONTROL_AT_INDEX[n] ? And then how can I edit it even more like change the content of a label if it is a label or the event handler if it is a button ?
Also I want a solution for the ListBox as well please.
I just don't know how to access those controls once they are inside.
Assign to that controls x:Name and use that in your code behind.
This is naturally not valid for controls present in Templates and Styles.
Like Tigran already posted, is possible to assign an attribute to your controls in XAML:
<ListBox x:Name="myListBox"
// more properties here...
/>
Then your code-behind will then be able to compile your line:
myListBox.Items.Add(new Button());
However, I strongly suggest you to alternativly use a MVVM approach to get rid of code-behind files. This reduces coupling of your business logic from the UI. Using the MVVM pattern is Microsoft's recommended way for working with WPF as it makes using many WPF feature very easy.
A great resource for Tutorials can be found int this SO thread, for example: MVVM: Tutorial from start to finish?
Here is my solution
var child = (from c in theCanvas.Children
where "someId".Equals(c.Tag)
select c).First();

WPF: change DataTemplate in resources from code

How can I change properties of some controls in DataTemplate that placed in resource from code?
I use .NET 3.5, so DataTemplate doesn't have Template property.
Take a look at DataTemplateSelectors
--Edit--
I'm sorry I did not understand the question at first, I think the best way would be to write an extra template, that looks the way you want to modify the other one, find it and assign it
to the list control.
Here is a little example

Possible to make value converter accessible only by my user control? (WPF)

So I've gotten in the habit of using WPF/C# value converters because they are awesome. I usually just have a folder set up for value converters and then access them in whatever xaml files I might need them.
I'm currently developing a user control that will have some value converters that I do not want other classes to be able to access. In other words, value converter exists in the code behind file and should be accessible only from that file and the associated xaml. My first thought was to throw it inside the code behind file as a nested class, but I can't find a way to access the nested class from within the xaml file. Am I going in the right direction or is there something else I should be doing?
I could go the cheap way and just throw this control into its own namespace, but that doesn't really solve my problem.
Any help or guidance is appreciated. Thanks!
Below is roughly what I want:
public partial class MyControl: UserControl
{
//misc code for control
private class MyValueConverter : IMultiValueConverter
{
//conversion functions
}
}
is what I have in mind.
Normally, value converters are accessed from WPF like:
`<local:MyValueConverter x:Key="MyValueConverter" />`
This only works if they are in the same namespace. I cannot get this to work if it is a nested class. I've met my goal of making the valueconverter visible only to this user control, but I cannot figure out for the life of me how to access it from within the xaml.
My problem is accessing this converter from in the xaml.
It is not possible (look here for the reason - 2nd paragraph about requirements)
Your custom class must not be a nested class. Nested classes and the "dot" in their general CLR usage syntax interfere with other WPF and/or XAML features such as attached properties.
You can of course work around that - but that limits you to setting the valueconverter in the code-behind (which is yucky :-))
I'm curious, though, why would you not want other classes to use it?
In your code-behind file, mark the methods that contain your converter code as private just like you would any other methods or properties that you wouldn't want to be visible outside of the class. Remember, the XAML and the code-behind file are (by default) both partial representations of the same class, so anything you declare in your code-behind file, even if private, is still visible to the corresponding XAML file.

Categories