I have a control I 'created' in XAML that I want to access through the Code Behind.
<wincontrols:LiveTileFrameElement Name="PendingAuthsFrame1" Text="Pending" />
this.PendingAuthsFrame1.Text = "334";
However, I get a Build Error on the second statement above stating MyApp.MainWindow does not contain a definition for 'PendingAuthsFrame1' and no extension method....
it keeps going but you get the idea I'm guessing.
What am I missing or doing wrong?
Use x:Name instead of Name. That should do the trick.
Related
In my application, I want to allow the user to load several arbitrary XAML files. Internally, I use XamlReader.Load() to load the XAML.
Now, it may happen that some of the XAMLs have named controls, such as
<Button Name="button" ... />
If the user accidentally loads 2 XAMLs that define the same name, an exception occurs, saying
Could not register named object. Cannot register duplicate name "button" in this scope.
So my next step was to unregister all names before loading the XAML:
foreach (FrameworkElement child in ((Grid) Content).Children)
{
if (!string.IsNullOrEmpty(child.Name))
{
UnregisterName(child.Name);
}
}
However, that's not sufficient, I still can't load the XAML for the same reason.
How do I get rid of the name of a control?
Note: while doing research on this, I found many answers for the question "How do I remove a control by its name?". That's not what I want. I want to keep the control, but un-name it.
Note: I'm not looking for workarounds. I know that I could e.g. process the XAML as plain XML, remove the names and then load it. I'd really like to know whether and how it's possible to do that in a WPF way.
I have also tried:
change the name with child.Name = null;
changing the scope with NameScope.SetNameScope(this, new NameScope());
combinations of the above
I'm currently dipping my toes into writing Universal Apps for the windows platform, for one of my attempts I'd like to try and make a one of these snazzy new single page apps.
However, I'd like to re-use my code where possible so that I follow DRY principles and don't repeat any code that I don't need to.
As such, I've made a number of User controls (which all work fine on their own) and I'm attempting to embed them inside an Xaml ContentControl ultimately allowing me to swap different ContentControl objects in and out of view at run time.
So far, in my experiments, I have the following code:
XAML:
<ContentControl x:Name="CentralContextHost" Style="{StaticResource ContentControlStyle}">
<!-- The following tag is which ever user control I wish to host -->
<Universal:CentralHubControl/>
</ContentControl>
And the code behind I use to manipulate the above XAML:
public object CurrentControl; // field
if (CurrentControl == null)
CurrentControl = new Profile(); // another usercontrol
var tempswap = CentralContextHost.Content;
CentralContextHost.Content = CurrentControl;
CurrentControl = tempswap;
I would however like to try and make my code much cleaner by using something similar to the binding syntax I've seen used elsewhere by possibly creating my controls as static resources in a dictionary of some kind xaml object, then just dropping a similar deceleration to a bind in the correct position in my xaml to have them display as needed.
The problem I have is that I'm not really sure how to approach solving this problem, or if there is even a way that it can be solved. My idea is to be able to do something similar to the following fictitious bit of code.
<Universal:CentralHubControl x:Key="CentralHub" />
<Universal:Profile x:Key="PersonProfile" />
<ContentControl
x:Name="CentralContextHost"
Grid.RowSpan="2"
Content="{StaticResource CentralHub}"
Style="{StaticResource ContentControlStyle}">
This seems to work in blend but crashes badly when used at run time.
can anyone here see why that might be or what I might be doing wrong?
If you need any more info please ask in the comments and let me know, I'd love to find an answer to both questions here.
I've not played with an app that has everything in one page myself yet. If I did, perhaps I might suggest using a panel as your content host instead of a contentcontrol.
For example, if you were to have a Grid as the root of your layout, which by default it usually is. You can then add your user controls directly to the panel in the code behind by using the following line of code:
rootLayout.Children.Add(AnyUIElement).
Doing things this way may make it possible to create a new instance of the wanted user control and potentially maintain state for it.
For an MVVM style solution, I'll have to play around with the concept.
Is there a specific reason you need to use a contentcontrol that I may not have considered?
I created a child template from visual studio to manage my kentico template. The template worked fine except that all the controls in it is not accessible from code behind because it is not recognized. I have checked for on line solution but none solved the issue. I even got this link
Codebehind file doesn't recognize aspx-controls
without any luck since I can't even access the myfile.aspx.designer.cs.
What do I trie again?
Note: My controls are not inside any panel or other control. Just inside a normal div.
Try adding runat="server" to your id tag.
Here is an example.
<tr class="headerrow" id="tbrHeader" runat="server">
I am unfamiliar with Kentico, but this is what I have come up with as possible solutions without having seen your code:
Make sure all your controls have a 'runat' attribute: runat="server". I am not trying to insult your intelligence, but it is an easy thing to forget(As I have done before), without this the control won't be recognised from code-behind
If it is a template file, have you made sure the codebehind that refers to it, is the code-behind of the template file, as the codebehind of other pages will not be able to find the control in the template without you telling them where it is.
With your new comment on the question: If your class is abstract, have you tried wrapping it in a non-abstract class? (source: stackoverflow.com/questions/481305)
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 :)
I did type runat="server" in the label tag. its still not accessible.
I did copy this label from another webform. I've noticed when copying labels from others webforms, sometimes they are not accessible. What is the problem?
Check your designer code and see if its in there. If its not your markup and designer are out of sync unless of course you have the control in a template. I have ran into this issue recently and fixed it by just adding a literal control forcing the designer to regen and then deleting the literal.
from what you have given here, I see you typed runat=server without quotations.
try adding quotations and check again.
runat="server"
full example
<asp:label runat="server" ID="Label1" >Label1</asp:Label>
It's because your code behind class is missing reference to that control. You guess you dont have .designer with your page class, right? Then you have to "map" that control manually
YOu can definie class variable like Label myLabel and then later in Page_Load you have to use myLabel = Find('myLabelId') function, to map that label. (This might not be 100% accurate syntax).
Edit: Asuming your label has ID="Label2", code should look like:
Label _label2;
Page_Load(
// some code
_label2 = (Label)FindControl("Label2");
)