Change default xml namespace(xmlns) to a custom one - c#

In a XAML file we usually use the default setting
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
How can I define a custom xmlns instead of the default one. Such as
xmlns="http://www.mypage.com"
I don't mean add to a new xmlns, but replace the default one.

It's not really clear what your problem is... if you want to use the default namespace somehow different than suggested, just prefix the (normally) default namespace with a name:
<def:Window x:Class="WpfTests_2.MainWindow"
xmlns:def="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="clr-namespace:WpfTests_2"
Title="MainWindow">
<def:Window.Resources>
<CustomBox x:Key="myLocalCustomBox"/>
</def:Window.Resources>
<def:Grid x:Name="grid1">
</def:Grid>
</def:Window>
In this example, I make the project local namespace WpfTests_2 the default and name the (normal) default namespace as def.
For a typical WPF application, this is a bad idea because you will use many controls and you need to prefix your namespace name every time. But it is certainly possible.
If you don't use anything from the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace (no Window, no UserControl, no [whatever control]), you can remove/replace it without redefining it with a prefix.

I think it is possible, by second part doc below:
For almost all of the examples given in the WPF sections of the SDK, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation.
Since almost all , That is there exists few example are not using this url, so it's possible.
Docs Of Microsoft
The WPF and XAML Namespace Declarations
Within the namespace declarations in the root tag of many XAML files, you will see that there are typically two XML namespace declarations. The first declaration maps the overall WPF client / framework XAML namespace as the default:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
The second declaration maps a separate XAML namespace, mapping it (typically) to the x: prefix.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Mapping to Custom Classes and Assemblies
...
Another Doc:
The root element also contains the attributes xmlns and xmlns:x. These attributes indicate to a XAML processor which XAML namespaces contain the type definitions for backing types that the markup will reference as elements.
The xmlns attribute specifically indicates the default XAML namespace. Within the default XAML namespace, object elements in the markup can be specified without a prefix. For most WPF application scenarios, and for almost all of the examples given in the WPF sections of the SDK, the default XAML namespace is mapped to the WPF namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation.
The xmlns:x attribute indicates an additional XAML namespace, which maps the XAML language namespace http://schemas.microsoft.com/winfx/2006/xaml.
This usage of xmlns to define a scope for usage and mapping of a namescope is consistent with the XML 1.0 specification. XAML namescopes are different from XML namescopes only in that a XAML namescope also implies something about how the namescope's elements are backed by types when it comes to type resolution and parsing the XAML.
Note that the xmlns attributes are only strictly necessary on the root element of each XAML file. xmlns definitions will apply to all descendant elements of the root element (this behavior is again consistent with the XML 1.0 specification for xmlns.) xmlns attributes are also permitted on other elements underneath the root, and would apply to any descendant elements of the defining element. However, frequent definition or redefinition of XAML namespaces can result in a XAML markup style that is difficult to read.
The WPF implementation of its XAML processor includes an infrastructure that has awareness of the WPF core assemblies. The WPF core assemblies are known to contain the types that support the WPF mappings to the default XAML namespace. This is enabled through configuration that is part of your project build file and the WPF build and project systems. Therefore, declaring the default XAML namespace as the default xmlns is all that is necessary in order to reference XAML elements that come from WPF assemblies.

Related

c# wpf, is xaml nested property/controls inheritance possible?

<Grid Name="mainSceneGrid" Grid.Row="1" Background="#FF075035">
<Grid Name="navigationGrid">
......
</Grid>
</Grid>
Normally from the code behind we would directly call navigationGrid.xxx which is very simple and effective. However when we start to get alot of dependent and nested grids for example it would be awesome to instead be able to call a property like this:
mainSceneGrid.navigationGrid.xxx
So we get some form of structured code and easier intellisense to work with, is this possible with xaml?
You misunderstand what the names of UI elements are.
For XAML, firstly, it is registering a name in the visual tree of elements using the FrameworkElement.RegisterName (String, Object) method and then searching in this tree using the FrameworkElement.FindName (String) method.
This is mainly used for bindings of type ElementName.
These names must be unique within the scope of the names.
Secondly, the x: Name Directive creates, in addition to the name in the visual tree, a field in the "* .g.i.cs" file.
This file is the XAML reflection generated by Studio Designer.
You can view it if you place the cursor on the call to the InitializeComponent () method and press F12.
It automatically changes when you change the XAML code.
And when you reference the name of an element in Code Behind, you are not actually referring to the name in XAML, but to a field in that file.
And if you know even a little about Sharpe, then you understand that you cannot create fields with names like name1.name2.
As for your problem, its origins are that you are not working with WPF in a way that is typical for WPF.
WPF is designed around the concept of UI elements getting values ​​for their properties on their own through bindings.
Therefore, using WPF element names other than ElementName bindings is a sign of incorrect implementation.
Such incorrect implementation methods are fraught with the occurrence of specific tasks, code bloat, and a high probability of bugs and errors.
I advise you, especially at the initial stage of training, in general, do not even open the "* .xaml.cs" files.
There should be nothing in the Code Behind other than calling the InitializeComponent () method.
When you learn the basics of WPF (layout, bindings, data context, styles, templates, triggers, converters), then you will be able to consciously decide on the use of Code Behind.
But in practice, there are almost no such tasks where it is really needed.

WPF name of image [duplicate]

Sometimes it seems that the Name and x:Name attributes are interchangeable.
So, what are the definitive differences between them, and when is it preferable to use one over the other?
Are there any performance or memory implications to using them the wrong way?
There really is only one name in XAML, the x:Name. A framework, such as WPF, can optionally map one of its properties to XAML's x:Name by using the RuntimeNamePropertyAttribute on the class that designates one of the classes properties as mapping to the x:Name attribute of XAML.
The reason this was done was to allow for frameworks that already have a concept of "Name" at runtime, such as WPF. In WPF, for example, FrameworkElement introduces a Name property.
In general, a class does not need to store the name for x:Name to be useable. All x:Name means to XAML is generate a field to store the value in the code behind class. What the runtime does with that mapping is framework dependent.
So, why are there two ways to do the same thing? The simple answer is because there are two concepts mapped onto one property. WPF wants the name of an element preserved at runtime (which is usable through Bind, among other things) and XAML needs to know what elements you want to be accessible by fields in the code behind class. WPF ties these two together by marking the Name property as an alias of x:Name.
In the future, XAML will have more uses for x:Name, such as allowing you to set properties by referring to other objects by name, but in 3.5 and prior, it is only used to create fields.
Whether you should use one or the other is really a style question, not a technical one. I will leave that to others for a recommendation.
See also AutomationProperties.Name VS x:Name, AutomationProperties.Name is used by accessibility tools and some testing tools.
They are not the same thing.
x:Name is a xaml concept, used mainly to reference elements. When you give an element the x:Name xaml attribute, "the specified x:Name becomes the name of a field that is created in the underlying code when xaml is processed, and that field holds a reference to the object." (MSDN) So, it's a designer-generated field, which has internal access by default.
Name is the existing string property of a FrameworkElement, listed as any other wpf element property in the form of a xaml attribute.
As a consequence, this also means x:Name can be used on a wider range of objects. This is a technique to enable anything in xaml to be referenced by a given name.
x:Name and Name are referencing different namespaces.
x:name is a reference to the x namespace defined by default at the top of the Xaml file.
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Just saying Name uses the default below namespace.
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x:Name is saying use the namespace that has the x alias. x is the default and most people leave it but you can change it to whatever you like
xmlns:foo="http://schemas.microsoft.com/winfx/2006/xaml"
so your reference would be foo:name
Define and Use Namespaces in WPF
OK lets look at this a different way. Say you drag and drop an button onto your Xaml page. You can reference this 2 ways x:name and name. All xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" and
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" are is references to multiple namespaces. Since xaml holds the Control namespace(not 100% on that) and presentation holds the FrameworkElement AND the Button class has a inheritance pattern of:
Button : ButtonBase
ButtonBase : ContentControl, ICommandSource
ContentControl : Control, IAddChild
Control : FrameworkElement
FrameworkElement : UIElement, IFrameworkInputElement,
IInputElement, ISupportInitialize, IHaveResources
So as one would expect anything that inherits from FrameworkElement would have access to all its public attributes. so in the case of Button it is getting its Name attribute from FrameworkElement, at the very top of the hierarchy tree. So you can say x:Name or Name and they will both be accessing the getter/setter from the FrameworkElement.
MSDN Reference
WPF defines a CLR attribute that is consumed by XAML processors in order to map multiple CLR namespaces to a single XML namespace. The XmlnsDefinitionAttribute attribute is placed at the assembly level in the source code that produces the assembly. The WPF assembly source code uses this attribute to map the various common namespaces, such as System.Windows and System.Windows.Controls, to the http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace.
So the assembly attributes will look something like:
PresentationFramework.dll - XmlnsDefinitionAttribute:
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Data")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Navigation")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Shapes")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Documents")]
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "System.Windows.Controls")]
They're both the same thing, a lot of framework elements expose a name property themselves, but for those that don't you can use x:name - I usually just stick with x:name because it works for everything.
Controls can expose name themselves as a Dependency Property if they want to (because they need to use that Dependency Property internally), or they can choose not to.
More details in msdn here and here:
Some WPF framework-level applications
might be able to avoid any use of the
x:Name attribute, because the Name
dependency property as specified
within the WPF namespace for several
of the important base classes such as
FrameworkElement/FrameworkContentElement
satisfies this same purpose. There are
still some common XAML and framework
scenarios where code access to an
element with no Name property is
necessary, most notably in certain
animation and storyboard support
classes. For instance, you should
specify x:Name on timelines and
transforms created in XAML, if you
intend to reference them from code.
If Name is available as a property on
the class, Name and x:Name can be used
interchangeably as attributes, but an
error will result if both are
specified on the same element.
X:Name can cause memory issues if you have custom controls. It will keep a memory location for the NameScope entry.
I say never use x:Name unless you have to.
Name:
can be used only for descendants of FrameworkElement and FrameworkContentElement;
can be set from code-behind via SetValue() and property-like.
x:Name:
can be used for almost all XAML elements;
can NOT be set from
code-behind via SetValue(); it can only be set using attribute
syntax on objects because it is a directive.
Using both directives in XAML for one FrameworkElement or FrameworkContentElement will cause an exception: if the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.
The only difference is that if you are using user Controls into a control from Same Assembly then Name will not identify your control and you will get an error " Use x:Name for controls in the same Assembly".
So x:Name is the WPF versioning of naming controls in WPF. Name is just used as a Winform Legacy. They wanted to differentiate the naming of controls in WPF and winforms as they use attributes in Xaml to identify controls from other assemblies they used x: for Names of control.
Just keep in mind dont put a name for a control just for the sake of keeping it as it resides in memory as a blank and it will give you a warning that Name has been applied for a control buts its never used.
x:Name means: create a field in the code behind to hold a reference to this object.
Name means: set the name property of this object.
I always use the x:Name variant.
I have no idea if this affects any performance, I just find it easier for the following reason.
If you have your own usercontrols that reside in another assembly just the "Name" property won't always suffice. This makes it easier to just stick too the x:Name property.
It's not a WPF item but a standard XML one and BtBh has correctly answered it, x refers to the default namespace. In XML when you do not prefix an element/attribute with a namespace it assumes you want the default namespace.
So typing just Name is nothing more than a short hand for x:Name. More details on XML namespaces can be found at link text
The specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed, and that field holds a reference to the object. In Silverlight, using the managed API, the process of creating this field is performed by the MSBuild target steps, which also are responsible for joining the partial classes for a XAML file and its code-behind. This behavior is not necessarily XAML-language specified; it is the particular implementation that Silverlight applies to use x:Name in its programming and application models.
Read More on MSDN...
When you declare a Button element in XAML you are referring to a class defined in windows run time called Button.
Button has many attribute such as background, text, margin, ..... and an attribute called Name.
Now when you declare a Button in XAML is like creating an anonymous object that happened to have an attribute called Name.
In general you can not refer to an anonymous object, but in WPF framework XAML processor enables you to refer to that object by whatever value you have given to Name attribute.
So far so good.
Another way to create an object is create a named object instead of anonymous object. In this case XAML namespace has an attribute for an object called Name (and since it is in XAML name space thus have X:) that you may set so you can identify your object and refer to it.
Conclusion:
Name is an attribute of a specific object, but X:Name is one attribute of that object (there is a class that defines a general object).
One of the answers is that x:name is to be used inside different program languages such as c# and name is to be used for the framework. Honestly that is what it sounds like to me.
My research is x:Name as global variable. However, Name as local variable. Does that mean x:Name you can call it anywhere in your XAML file but Name is not.
Example:
<StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />
<Button Content="Example" Name="btn" />
</StackPanel>
<TextBlock Text="{Binding Path=Content, ElementName=btn}" />
You can't Binding property Content of Button with Name is "btn" because it outside StackPanel
Name can also be set using property element syntax with inner text, but that is uncommon. In contrast, x:Name cannot be set in XAML property element syntax, or in code using SetValue; it can only be set using attribute syntax on objects because it is a directive.
If Name is available as a property on the class, Name and x:Name can be used interchangeably as attributes, but a parse exception will result if both are specified on the same element. If the XAML is markup compiled, the exception will occur on the markup compile, otherwise it occurs on load.

How can I automatically verify that all controls in my views are set with binding?

Looking for a way to automatically verify, using unit-tests for example, that all controls in my WPF application are set to Binding.
I'm using data-binding to enable localization in the application.
For example, a TextBlock control in a view will have it's Text property binding to 'SomeLocalizedLabel' property in the view model.
I need to make sure that I won't miss any control, and all my 'TextBlock' controls have their 'Text' property bound to something.
I can check the correctness of the localization itself in the ViewModel level.
I can check that all public ViewModel properties are bound to something in the view using the Caliburn.Testability assembly or some other method.
The thing that missing here is to test the View level.
UI testing framework is not the solution I'm looking for, because I wish for something that can be written and executed fast.
See my post here:
How to check if a XAML element supports AutomationId attribute
You could use the mechanisms mentioned to go through all your XAML and look at the Text properties of elements, and decide if they have missed a Binding.
LocBaml is a tool that goes through the XAML to extract localizable properties....your rolling your own localization technique...but I'm sure you can adapt that to extract check "Text" properties and see if they have a binding, then produce a report where they are not.

Errors while porting code to silverlight

I have a C# class library project in my solution, in which I added a User Control and pasted the following code from my WPF application.
The xaml looks all fine, but I am getting certain errors on compiling. For eg:
1) System.Windows.DataFormats' does not contain a definition for 'StringFormat'
2) System.Windows.Controls.TextBlock' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'System.Windows.Controls.TextBlock' could be found
3) System.Windows.Media.Colors' does not contain a definition for 'LightCoral'
4) System.Windows.Controls.TextBlock' does not contain a definition for 'Background' and no extension method 'Background' accepting a first argument of type 'System.Windows.Controls.TextBlock' could be found (are you missing a using directive or an assembly reference?)
I am not sure why these errors are occuring and these are related to TextBox's background, Drag Drop, DataFormats etc. The .xaml.cs code is here as well. http://pastebin.com/CY8E8prz
You have to remove the properties referred to by the errors, because Silverlight simply doesn't have them or they have a different name (WPF and Silverlight aren't exactly the same, the former has more features than the latter).
A few examples:
To color the background of a TextBlock, you can wrap it in a Border (adjusting positioning/sizing attributes accordingly).
LightCoral looks like a color name, you'll have to look up something similar or use Color.FromArgb()
In short, copy-pasted code from WPF to Silverlight very rarely works as-is.
Textblock has no background in Silverlight, only WPF. I'm sure the rest of your errors follow suit. For that particular issue, place the textblock over another control or use a label instead. For the color, just look up the RGB and set it directly. I'm not sure on the other errors exactly.

Creating a DataTemplate in Code: Can I use the Template property?

I'm attempting to use a DataTemplateSelector with a particular third-party WPF grid control, and I'm having trouble determining if the issues I'm having are a bug in the control or my own lack of understanding about the conventions of WPF data templates.
I realize that the ordinary use case of a DataTemplate is to declare it somewhere in XAML (be it as a resource or explicitly where it's used), but my particular project would benefit greatly if I could create the template in code (C#, specifically) rather than in XAML. The issue I'm running into is the fact that my code-created DataTemplate uses a FrameworkElementFactory as the template's VisualTree, whereas a XAML-created template uses a TemplateContent object as the template's Template value. As best I can tell right now, the grid control in question works with templates that use Template, but doesn't seem to play nicely with templates that use VisualTree.
By way of comparison, here's what one of the templates looks like in XAML as part of my selector:
<MySelectorType>
<MySelectorType.BooleanTemplate>
<DataTemplate>
<EditorControl Name="Reserved_Name" />
</DataTemplate>
</MySelectorType.BooleanTemplate>
</MySelectorType>
And here's how I'm trying to create an equivalent template in code:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
{
Name = "Reserved_Name"
}
};
I've also tried it like this:
var template = new DataTemplate()
{
VisualTree = new FrameworkElementFactory(typeof(EditorControl))
};
template.VisualTree.SetValue(EditorControl.NameProperty, "Reserved_Name");
Which seemed more analogous to what the XAML template would do, but that appeared not to work at all (the editor neither read or set the value, where at least the first version would read it).
Is it possible for my in-code template to use the Template property rather than VisualTree? According to the documentation, there's no public API for this type and the instantiation path is complex, but has this been done? The only example I've found uses hardcoded XAML in the code, which doesn't sit well with me.
I do not like this way of doing things either but this actually is the recommended way, in the documentation of the FrameworkElementFactory the following can be found:
This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class.
I do not know of any simple way to use the Template property in code, the only way to my knowledge that might be possible is via a lot of reflection.
Setting names is a special case, if you set the Name property of the factory it should be properly registered, if not you need to get the approriate Namescope and register the name manually.

Categories

Resources