Why is Grid.Row attached property specified without any enclosing Grid? - wpf

I am reading code from a WPF project which has the following XAML code (I have omitted the boilerplate parts):
<Window x:Class="AdornedControlSample.Window1"
...
x:Name="window"
...
>
<Canvas
Grid.Row="1"
x:Name="canvas"
>
...
Please note that there is no Grid defined whatsoever anywhere in this XAML file. So, my questions are:
What is the purpose of the Grid.Row="1" within the Canvas definition?
What purpose does x:Name="window" serve? I have not seen anything in the code-behind that references a window. (There are references to Window1, though.)

Question 1
In your example, Grid.Row has no purpose at all. You gave it a value, but that value will not be used because there is no Grid around the Canvas. It also doesn't give an error because it is a valid property/value.
The RowProperty is defined in the Canvas class.
Question 2
It gives a unique name (within the window itself) to the UI-element. For now, it has no purpose. So you can remove it safely. However, if you want to access the window in the code-behind of the view, the x:name serves a purpose. But you can also access the window via the this keyword.

Related

How should I structure my WPF classes to represent a generic icon type with specific icon representations?

I have a WPF application where one of the main features is a 'layout' window. In the layout window, the user can place/position/resize LayoutElements.
The place/position/resize behavior of the icons is shared; they all act the same in terms of mouse-dragging and resizing. I've implemented this behavior and it's working as I intend.
An issue with my current approach is that I've both defined DataTemplates in my main Canvas window to tell the XAML how to render the various LayoutElement subclasses, and each LayoutElement subclass has its own XAML file to define how its appearance.
For example, in my main drawing/canvas XAML, I have:
<DataTemplate x:Key="trackTemplate">
<local:SizingOverlay MouseDown="IconMouseDown" MouseUp="IconMouseUp" MouseMove="IconMouseMove" MouseEnter="IconMouseEnter" MouseLeave="IconMouseExit">
<local:Track Width="128" Height="128" Canvas.Left="0" Canvas.Top="0" />
</local:SizingOverlay>
</DataTemplate>
The above is used by a template-lookup to say: when you have a Track LayoutElement, render it this way.
Additionally, I have Track.xaml which defines the look of the local:Track elements.
If I continue to add more LayoutElement types, I will have to keep adding more DataTemplates which basically say: for each element type, use the appropriate XAML file as the content for the SizingOverlay.
However, it seems like there should be a way to tell this DataTemplate that it contains LayoutElements, and it should use the contained concrete subclass of LayoutElement to get the XAML.
In other words: I have the abstract class LayoutElement and concrete representations of this class. How can I tell WPF to use the concrete subclass's XAML?
Here is what I've tried:
If I add an XAML layout to LayoutElement, then Track.xaml complains because it is a LayoutElement and it's conflicting with the LayoutElement.xaml definition.
If I don't have XAML for LayoutElement, then I can't do something like this:
<DataTemplate x:Key="trackTemplate">
<local:SizingOverlay MouseDown="IconMouseDown" MouseUp="IconMouseUp" MouseMove="IconMouseMove" MouseEnter="IconMouseEnter" MouseLeave="IconMouseExit">
<local:LayoutElement Width="128" Height="128" Canvas.Left="0" Canvas.Top="0" />
</local:SizingOverlay>
</DataTemplate>
Edit: Possible duplicate
I acknowledge this question is potentially a duplicate of the following:
How to create a common WPF base window style?
Abstract class on XAML-Window
Creating an abstract base class for windows in WPF
c# WPF XAML - Working with abstract UserControls
MyUserControl cannot be the root of a XAML file because it was defined using XAML
Inheriting from a UserControl in WPF
Using an Abstract Class as DataType in DataTemplates
However, I am not sure if the questions above contain complete/appropriate answers for my use-case. They contain quite a bit of hypothetical discussion with the answers saying "I think" this, "maybe" that.
I believe I've tried the solutions proposed in the other links (particularly the last one) and it compiled, but the concrete classes were not displayed as if it couldn't find the concrete class XAML and just gave up.
I will examine those answers, and if it's clear that they apply to this question, I will delete this question. Otherwise I will update this question with a new answer (or someone else can).
I was able to get it working by replacing my abstract-class XAML element with a ContentPresenter whose content is bound to the child class.
<DataTemplate x:Key="elementTemplate" >
<local:SizingOverlay MouseDown="IconMouseDown" MouseUp="IconMouseUp" MouseMove="IconMouseMove" MouseEnter="IconMouseEnter" MouseLeave="IconMouseExit">
<ContentPresenter Content="{Binding}"/>
</local:SizingOverlay>
</DataTemplate>

How to set WPF default values for properties, if possible?

I am just starting WPF and it is frustrating the hell out of me. It seems that many properties are null by default (at least those I am working on at the moment) and hence when it compiles and run, nothing happens.
Is there a quick way or a standard workflow procedure to set default values for WPF objects?
For example, I put a Canvas and a Button in XAML view, and then went to code view to add an event handler on the Button to Canvas.Children.Add(new Ellipse()) and then nothing happens. Then I thought maybe I should specify the Width and Height. Still nothing happens. Finally, after much struggling I found the Shape.Stroke property.
Then there is no intuitive Ellipse.X and Ellipse.Y to position the Ellipse. Again, took an hour to find the Canvas.SetLeft().
The final straw is when I try to do Canvas.SetLeft(Random.Next(0, (int)Canvas.Width)); It give a runtime error because Canvas.Width is NULL?!!? Goodness...
Sure, WPF gives a lot of features, but seems like a lot of work coming from a Winforms Graphics.DrawEllipse() .. *sweat*
In WPF if you dont explicitly set the Width/Height in xaml the size will be determined by the Elements layout Container, so to access the Width/Height of an Element like this you use the properties ActualWidth/Actualheight, these return the Rendered size of the Element
Example:
Canvas.SetLeft(Random.Next(0, (int)Canvas.ActualWidth));
If you want to create Default values for a Element you can create a style in xaml for that Element
Example:
<Style TargetType="Ellipse">
<Setter Property="Stroke" Value="Black"/>
</Style>
WPF does have a rough learning curve. One of the tougher things is to dispense somewhat with the techniques you may be used to and embrace the WPF-approach. Xaml is the way to go for defining controls and their properties - Xaml is a language whose only real purpose to do declaration well. In essence, think of the Xaml portion of your code as a glorified constructor.
<Window x:Class="TestWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window"
Title="MainWindow"
Width="640"
Height="480">
<Canvas>
<Ellipse Canvas.Left="50"
Canvas.Top="50"
Width="142"
Height="88"
Fill="Black" />
</Canvas>
</Window>
The declaration above takes advantage of Xaml's nifty syntax for Attached Properties.
You might want to investigate Styles if you find yourself setting a set of common properties on like objects often.

How do I break a large XAML file into sub-XAML files and maintain communication between the parent and child objects?

I'm new to WPF. I'm attempting to modify the project VisualStudioLikePanes from the book WPF 4 Unleashed. Because the panes are hidden by default until I run the project, I decided that it would be nice to place the pane I'm working on into a separate xaml file so that I can see the changes I make to the pane without needing to launch the executable.
So, based on some posts I read here on StackOverflow a few days ago, I added a new UserControl to the sample project and plopped the content of the pane in question into that. Here is what the UserControl attributes look like in the 'child' XAML file:
<UserControl x:Class="Sample.SettingsPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d">
To include this control into the parent, I added the xmlns:sp namespace to the 'parent' XAML file:
<Window
Title="MainWindow"
x:Uid="Window_1" x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sp="clr-namespace:Sample"
x:Name="window">
I then 'included' the control via this:
<sp:SettingsPanel Visibility="Collapsed" x:Name="layer1" x:FieldModifier="private" />
I immediately found that in the code-behind file for the 'parent' XAML file, all of the code which made reference to any of the elements now contained in the 'child' XAML file were now unrecognized. So, I then removed (or commented out) all references to names and objects which were now contained within the 'child' XAML file and ever since then have been jumping through hoops to wire things back up.
For example, I want one TextBox in the 'child' XAML file to reflect what is in a TextBox in the 'parent' XAML file. I believe that the following binding would work, but, of course, I can't place this into the 'child' XAML, because it doesn't 'know' about the parent's 'test' element any longer.
<TextBox Text="{Binding ElementName=test, Path=Text}" />
I'm sure I've broken up the 'parent' XAML file incorrectly. I can't imagine that everytime somebody wants to break some segment of XAML out to another file they must rework all of their code behind and set up special communication hacks to let elements continue to communicate.
I did look at a variety of posts (e.g. Binding two UserControls to the same DataContext, or ViewModel? and What is the easiest way to break up large XAML files in my application?), but they didn't address my particular question.
Thanks,
Matt
When you split out your elements to a UserControl you can still access them by x:Name field value you provided. However, since you are new to WPF I would start looking into the MVVM pattern before you develop any "bad habits". It specifically addresses your concerns.

What does (x) in x:Name mean in Silverlight XAML Markup?

We see some properties in TextBlock or Grid like this:
<TextBlock x:Name="TextBlock1" ...
Why do we include this (x)? why don't we just say:
<TextBlock Name="TextBlock1" ...
I mean, we're already within the definition scope of this TextBlock, right?
There must be a reason for that.
Thanks in advance.
As an extension to Gabe's answer, x:Name is an attached property. Attached properties are different from standard properties, as they aren't defined (usually) on the control that uses them. For example, the TextBlock control does not have an x:Name property - instead, this property is defined elsewhere (in the XAML namespace), and is being "attached" to the TextBlock control to implement it's behaviour. It's saying "I want to use the Name attached property that can be found in the XAML namespace). Of course, to complicate things, the TextBlock control has a Name property (it didn't used to in Silverlight 2, thus you needed to use the x:Name attached property instead). They do the same thing though.
Another (easier to understand) example of an attached property is Grid.Row. You can use this property on the TextBlock control to specify what row the control should appear in a Grid, even though it's not defined on that control (the Grid control defines it). The TextBlock is simply attaching that property to itself, which associates itself with that behaviour. It's a confusing concept initially, but very powerful and useful. More info on attached properties can be found here: http://msdn.microsoft.com/en-us/library/cc265152(VS.95).aspx.
Hope this helps...
Chris
That is a namespace prefix.
Example 1:
You should see something like this on the xaml page:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Which declares the x prefix referring to the xaml namespace.
Example 2:
You could load your own user controls by registering the namespace and giving it a prefix.
xmlns:mycontrols="clr-namespace:MyControls.Namespace;assembly=MyAssembly"
Then here we are using the prefix to utilize one of the controls from this namespace.
<mycontrols:MyControl />

How to access a WPF control located in a ControlTemplate?

Usually, the WPF controls are declared in the .xaml files and not in the code behind (.xaml.cs files). However, sometimes I need to use some of those controls in code behind in order to manipulate them. How can I get the handle of such a control if it "resides" in the xaml file?
You can use the FindName() method of the ControlTemplate class.
// Finding the grid that is generated by the ControlTemplate of the Button
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
I'm unsure about what you're asking, so I'll try and answer both instances that I'm interpreting as your question.
1)
If you want to declare an explicit control, and then edit it directly, all you have to do is set the name property like such:
<Canvas x:Name="myCanvas"/>
You can then access the canvas through the Name as such:
myCanvas.Background = Brushes.Blue;
2)
If you're looking to declare a generic control, and then use it multiple times, you can do it like this:
<Window>
<Window.Resources>
<Ellipse x:Key="myEllipse" Height="10" Width="10">
</Window.Resources>
</Window>
You can then access that predefined control using this syntax in code:
Ellipse tempEllipse = (Ellipse)FindResource("MyEllipse");
If you want to use the Resourse as a template for multiple controls, add x:Shared="false".

Resources