Windows 8 - getting a reference to the main Page from a UserControl - xaml

In my sample Windows 8 app I have a Popup UserControl serving as a custom SettingsPane where I want to let to choose MainPage background color.
The problem is I can’t access MainPage’s children from UserControl’s code behind file - I named the root grid and I still get the error The name “mainGrid” doesn’t exist in the current context. I know this issue has been solved for WPF but none of these solutions seems to work for a Windows 8 app.

Simply pass a reference to Main when you use the user control. Have this as a property on your user control that you set at runtime.
Another option is that if you need to do operation X when something happens in the user control you can simply define an event in your user control, and implement an event handler for that event in your MainPage.

Related

VB.NET Win. Form - Showing a custom control outside of the container control

I have a created a custom user control which consists of 2 controls:-
Textbox
Listbox
The function of this control is to act as a dropdown list. Below is the image of the control:-
Problem
Now the problem i am facing is that if i insert this user control into a container control like a panel then the list gets hidden inside that container control.
and if i just create the user control outside the container control then it would interfere with the Tab Order (focus order) of the form.
Is there any work around where the user control exists in the container control and still shows the complete list without being hidden in the panel?
Edit i wrongly added c# instead of VB.NET
It can be achieved by having the list as ToolStripDropDown. Similar customization have been done in the following discussion,
Show control inside user control outside the boundaries of its parent
Hope this suits you.

WP8 ListPicker w/ SelectedValue and SelectedValuePath?

I'm working on a Windows Phone 8 app, which is basically a port of my existing Silverlight app. On one screen in the SL version, I have 10 ComboBox controls that default to nothing selected, forcing the user to make a selection before saving the item. That works great (this is my company's bug tracking application, so I want the users to be forced to make these selections, else they'll leave in a default, which will likely be incorrect).
In WP8, the powers that be seem to want us to use their ListPicker from the WPToolkit (since there is no ComboBox there or in the default controls). However, this is more a ListBox that supports SelectedIndex but not SelectedValue and SelectedValuePath. I've been using an index property in my viewmodel(this is kind of a pain since the model loads async, but I've been working around that..), but I have to default it to an actual option, which negates the business rule of forcing a selection.
I've also tried essentially copying the source code for ListPicker and have it inherit from Selector (the base control just inherits from ItemsControl), but that won't work as the Selector constructor is internal.
It seems like what I want to do is add support for SelectedValue and SelectedValuePath. Is that possible? Or am I going about this the wrong way?

Nested UserControls in Windows Phone

I am trying to create a Windows Phone app that will take use a similar UI element in multiple user controls.
One user control is loaded by the main xaml and this user control consists of another user controls. The user controls are all in the same directory below where the main xaml exists.
I get an XamlParseException on the following line in my g.i.cs file for the nested control:
System.Windows.Application.LoadComponent(this, new System.Uri("/MyApp;component/UI/NestedControl.xaml", System.UriKind.Relative));
I think it is doubling the component/UI portions of the path as the parent user control already resides in this directory.
Does anyone know how to solve this?
This exception typically means that the XAML parser cannot find the referenced XAML, or that you have invalid XAML for your NestedControl class.
Without further code, it is hard to determine the root cause. I would suggest deleting these classes until your project compiles, then slowly adding them back again, compiling each time. This will help you identify where the problem lies.

SharePoint 2010: trying to understand life-cycle issues with visual web part

I am building a visual web part which has a number of user-configurable properties. The web part will display news items and, optionally, automatically switch to the next item after a defined pause.
The problem I'm having is that the user control part has a timer which fires events. These events reenter the user control (without going through the web part) and I see that when this happens the user control is in an unitialised state - i.e. the custom properties of the web part have not been applied. This causes the control to behave in it's default manner.
What am I doing wrong?
When you set property in your webPart, you need to bubble it down to the User control.
See creating custom properties for visual webparts:
http://patricklamber.blogspot.com/2010/05/how-do-i-create-custom-properties-in.html

vb.net - problem making a call from one usercontrol to another form

I have a tabcontrol that creates tab pages from a "User Control" I created (a separate form in vb.net) using this code: (MainTab is the separate user control I created which has text boxes etc in it)
Dim tmpTab As New MainTab
myTabControl.TabPages.Add()
Dim tmpTabCount As Integer = myTabControl.TabPages.Count
myTabControl.TabPages.Item(tmpTabCount - 1).Controls.Add(tmpTab)
myTabControl.TabPages.Item(tmpTabCount - 1).Text = "Untitled"
I'm using the devexpress xtratab control so the code might look a bit different than the default vb.net tab control.
Now in my MainTab user control file file, I can't for the life of me figure out how to call a control in the form1 where the xtra tab control is placed on. "Me.Parent.Dispose" works for closing the tab when executed via the MainTab control, but that's as far as I can get for communicating with the parent from.
Does anyone know the solution? I'm not sure if I have to reference something in the MainTab user control or what in order to communicate with any objects on the default form1.
Generally speaking, I avoid making my child controls cognizant of the parent. It leads to unpleasant coupling more often than I care for.
Consider adding a custom event to your MainTab class that your form can subscribe to. When you want to pass a message to the form, your user control can invoke the method, and your form's event handler can process it accordingly. This pattern helps keep your user control pluggable into other forms by reducing its dependency on its parent.
Creating a user control event in a windows form is discussed in this MSDN article:
http://msdn.microsoft.com/en-us/library/aa302342.aspx