Hi everybody!
Alright - so if you've read my other posts, you'll know that I'm working on a WYSIWYG-type graphics editor - and I've been dealing with naming issues with regards to having to name and clone thousands of elements on a canvas. Also in my application, when I drag and stretch, I create a ghost of the base element. This also required cloning, and up until now, namespace checks.
I was aware that a user control actually creates it's own namespace, such that you can have the same x:name is multiple controls. This is obviously a very, very handy thing to have. I wanted to be able to separate some content from the rest of my application easily, so that I could reduce the number of name checks I need to do as a result of cloning elements based on hardcoded strings.
Try 1: Creating an essentially empty UserControlOriginally, I had tried to create a helper UserControl called "SafeNamespace" which was essentially an empty control with a Canvas root.
[UserControl x:Class="Graphics.SafeNamespace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"]
[Canvas x:Name="LayoutRoot"]
[/Canvas]
[/UserControl]This was mildly annoying because I could not easily treat this like a canvas [to my knowledge] and I wrote small helper functions in the xaml.cs to allow for simplier setting and getting of the LayoutRoot content. It was just a little ugly - it worked, it was just icky.
Better Method: Extending the Canvas ClassAfter a little searching I came across
this forum post which talked about extending the Canvas class itself, instead of extending the UserControl class. This also allows you to implement custom functionality onto the Canvas if you desired, but for me I was really only looking for the ability to create a seperate namespace.
xaml:[Canvas x:Class="Graphics.SafeNamespaceCanvas"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"]
[/Canvas]and
xaml.cspublic partial class SafeNamespaceCanvas : Canvas
{ ... }In short, creating an extention of a class was a nice way for me to achieve some namespace independance... and was extremely handy given the large number of named elements I was dealing with! Just thought I'd pass along the idea :)
.