Error trying to access xaml "local:" control by name from codebehind - c#

This all began as an attempt to have a numeric textbox. That part is irrelevant but it's why I created the following class. (By the way, using VS 2012 Express, WPF, C# code-behind).
using System;
using System.Windows.Controls;
namespace Herculese
{
public class IntBox : TextBox
{
<!-- irrelevant code here-->
}
}
So far, so good. I build and this becomes a control which I proceed to use in the xaml:
<local:IntBox Name="txtBox_heightft" Width="60" TextChanged="txtBox_Numeric_Changed" />
Then in my code behind where I'm trying to refer to the text in the textbox using "txtBox_heightft.Text", I'm informed that "The name 'txtBox_heightft' does not exist in the current context". This confuses me to no end because if I change "local:IntBox" to "TextBox" in the xaml, it works fine but then of course it's a regular textbox and not my modified version. Do I need to add a reference to the class in the codebehind somehow? This is my first attempt at using a class this way, as I've never needed functionality that wasn't provided by default.

The problem is that you are using Name as a dependency property, you need to use x:Name="txtBox_heightft" as an extension property :)

Related

How can I use settings stored with Xam.plugins.settings in my XAML?

Thanks to James Montemagno for this plugin.[https://jamesmontemagno.github.io/SettingsPlugin ]
I have used it in my c# code easily: CrossSettings.Current.GetValueOrDefault("abc", "")
But I also want to use those settings in my XAML.
Before this plugin, I was using my own basic (inefficient) array setup and used: ...Text="{x:Static local:Settings.abc}"... Now I have completely converted over to this plugin. And it works well in c#, but am struggling to get it working in XAML.
I Tried {x:Static local:Helpers.Settings.GeneralSettings.abc}, and {x:Static helps:Settings.GeneralSettings.abc} (creating xmlns:help).
How can I use those settings in my XAML code?
The best way to do this is to set up a Property Accessor (get/set) - and bind the control to that method.
As a quick (one way) example..
Page Code behind
public int MyNumber
{
//This may be different, depending on what your Settings class has been named and where its reference has been stored - but it is the same plugin.
return App.Settings.GetValueOrDefault("myNumber",0);
}
XAML
<Label Text="{Binding MyNumber}"/>
You'll also want to make sure you've set the BindingContext. I find it easiest to do this after InitializeComponent(); in the page constructor, simply by using BindingContext = this;.
A more complex implementation (With two way data binding) can be found in the Settings Plugin Documentation.

The name 'foo' is not exist in namespace 'http://foo...'

I have an WPF solution and this solution consist of 3 project:
1-A project that has several WPF user control inside
2-Another project that has several WPF user control inside
3-A project which has Resources for 2 WPF projects above.
As you know, if you have common settings for you views like that
-Using Same FontFamily.
-Using same FontSize
-Using same FontWeight
-Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
So, In my Resource project, I adited AssemlyInfo.cs file like this:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
This edit gives me ability to declare/create a user control like below:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
From now, I do not have to create a usercontrol view which its tags start with
<UserControl...., I can start with <internalresources:PageBase......
My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:
-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"
P.S: References are added properly to my Project and My project was suitable to open with blend.

Move and rename MainPage.xaml

I am developing a Windows Store project, and I wish to move MainPage.xaml into a folder called Views. After that I want to rename it. I already attempted doing this, but I ended up breaking it (InitializeComponent could not find a definition).
How can I move and rename the page properly?
Actually, it seems that the MainPage type is hard-coded in the auto-generated code.
look at this post to get information on how to change it.
Let's say you renamed it to MyView.xaml, and moved it to Views folder.
You will probably want (not necessary) to also:
1. add ".Views" to the namespace in MyView.xaml.cs
2. add ".Views." to the x:Class tag in MyView.xaml
Now open App.cs, and locate the following line
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
....
}
change MainPage to reference your new page name.
Simply make sure to keep the namespaces between the code behind and the xaml synchronized:
MainPage.xaml.cs:
namespace YourNameSpace.Views
{
public partial class MainPage : UserControl
{
}
}
MainPage.xaml
<UserControl x:Class="YourNameSpace.Views.MainPage"
....>
</UserControl>
Quick note: It's a little different on Windows Phone: the startup page is defined in the DefaultTask section of the WMAppManifest.xml file:
<Tasks>
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>

Return a UserControl from a method that is not in the Code-Behind file.

I want to create a helper method that could be called from different code-behind files. This method would return a UserControl (myUC). However I do not see any way of accessing myUC except by Registering the UserControl in the ASPX file or in the web.config file, but neither of these methods will give me access in Non-Code-Behind CS files. I thought that maybe I could wrap the UserControl in a Namespace and then use that namespace in the CS file but that did not work either.
I am thinking of giving up on using the UserControl for a composite control ( http://msdn.microsoft.com/en-us/library/aa719734(v=vs.71).aspx ) as this can be wrapped in a Namespace and used in any CS file.
But before I do this I was wondering if anyone can shed more light on this?
Thank you.
Joseph
UserControl is not meant to be create from code behind. It is not its purpose. You have to use CustomControl for that. Read here:
ASP.Net Custom controls vs. user controls: Are these two the same

asp.net Cannot create UserControl in class

My ultimate goal is to create a UserControl in a class in my App_Code folder and render the html from that into a string.
The rending of HTML can be done using this example I believe:
How do I get the HTML output of a UserControl in .NET (C#)?
My Question:
I created my user control
public partial class Controls_MyUserControl : System.Web.UI.UserControl
I've registered it in my web.config
<add tagPrefix="UC" src="~/Controls/MyUserControl.ascx" tagName="MyUserControl" />
I can reference this on a page just fine
<UC:MyUserControl ID="MyUserControl1" runat="server" />
Yet when I try to create an object of this control type in a class (in my app_code folder) it doesn't allow me.
Controls_MyUserControl dummy = new Controls_MyUserControl();
The potentially strange thing is when I put this code into a code behind of a page, it works.
Any ideas on what I need to do to have the usercontrol to be able to be created in my class in the app_code folder?
My guess is, is that I need to reference the control in a "using" statement at the top, but I'm not sure what that'd be.
It's not in a namespace to my knowledge (at least there's no namespace, in the code behind of the actually user controls). Though I'm not sure if it inherits one from the System.Web.UI.UserControl.
Thanks in advance
Some workaround as posted by Scott Alen http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-app_code-folder.html
Suppose you have all your user controls in ~/UserControls/
Then on your code behind add the following;
using YourSpaceName.UserControls;
This will set reference to any user control in that location.
Note: if you are using a user control within another, make sure to create a folder for each of them. I have experiencing problems, specifically with VB where it would compile but give me a runtime error. Since I encounter that problem I create my user controls each in it's own folder.

Categories