I've created this simple UserControl, and my questions to you WPF experts are:
- Why I can't add content to it via Blend design-time?
- Why can't I edit the control? When I added the ContentTemplate and DataTemplate, POFF! Suddenly I can't edit it anymore...!?
Code Snippet for UserControl1.xaml
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="UntitledProject3.UserControl1" x:Name="UserControl" Width="100" Height="50">
<UserControl.ContentTemplate> <DataTemplate> <Grid x:Name="LayoutRoot"> <Rectangle Fill="#FF3C3C3C" RadiusX="5" RadiusY="5" x:Name="rectangle"> </Rectangle> <ContentPresenter Content="{TemplateBinding Content}"/> </Grid> </DataTemplate> </UserControl.ContentTemplate> </UserControl>
|
| azoapes Tuesday, May 13, 2008 6:22 PM |
OK, I misunderstood what you were trying to do. I thought you had problems editing your UserControl.xaml file with the designer as opposed to parenting into an instance of it in a different XAML file.
To do the latter, you can define your UserControl like you did in your first post but need to put a Grid into the instance of the UserControl so that it can accept children, e.g. like this:
Code Snippet
<uc:UserControl1>
<Grid>
</Grid>
</uc:UserControl1>
After this you will be able to drag controls into the Grid. |
| Marco Goertz - MSFT Monday, May 19, 2008 4:02 PM |
What is the purpose of adding
<UserControl.ContentTemplate> <DataTemplate>
Like Window, UserControl also comes initialized with a Grid, which you seem to have embedded inside of ContentTemplate and DataTemplate. Just remove them and add what ever you want to inside of the Grid and it should work just fine. |
| Atul Gupta Wednesday, May 14, 2008 3:54 AM |
| Atul Gupta wrote: |
|
What is the purpose of adding
<UserControl.ContentTemplate> <DataTemplate>
Like Window, UserControl also comes initialized with a Grid, which you seem to have embedded inside of ContentTemplate and DataTemplate. Just remove them and add what ever you want to inside of the Grid and it should work just fine. | | It feels like you did not read my questions, nor the XAML. I'll explain. My question number one said that I want to be able to add content into my UserControl. The XAML contains a <ContentPresenter> and for that to work, VS/Blend said I have to be inside of a Template. So I added UserControl.ContentTemplate but then the compiler says I have to add <DataTemplate> also, so I did ("the following type was expected: DataTemplate"). A UserControl can only contain one child, that's why I added the (extra?) Grid. But it doesn't matter if there's five Grids or not, it should work anyway... Please modify the XAML and post it here so I can compile it and see what you mean. |
| azoapes Wednesday, May 14, 2008 8:22 AM |
I've tried many variations of Templating but cannot solve this... can anyone help?
To clarify, I want to do this;
1) be able to drag for example a button INTO my UserControl in Blend/Visual Studio
2) be able to edit the control in the normal way in Blend (right-click, choose Edit Template etc.)
Any experienced UserControl developers? Question 1 is most important!
|
| azoapes Thursday, May 15, 2008 9:04 AM |
What are you trying to achieve? If your UserControl simply looks like the following you will be able to drag controls into it.
Code Snippet
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="UntitledProject3.UserControl1" x:Name="UserControl" Width="100" Height="50"> <Grid x:Name="LayoutRoot"> </Grid> </UserControl>
|
| Marco Goertz - MSFT Friday, May 16, 2008 2:51 PM |
| Marco Goertz - MSFT wrote: |
|
What are you trying to achieve? If your UserControl simply looks like the following you will be able to drag controls into it.
Code Snippet
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="UntitledProject3.UserControl1" x:Name="UserControl" Width="100" Height="50"> <Grid x:Name="LayoutRoot"> </Grid> </UserControl>
| | I copied your exact code into a new UserControl, compiled it and placed an instance of it onto a Window. I could NOT drag any objects into the UserControl.
Of course I could place a Button on top of it, but I want to be able to drag it INTO the UserControl, so that I can move the UserControl and the button will follow. So that I can let the UserControl expand depending on how much content it has. The standard way of doing this in Blend is to press the ALT key when hovering the container.
Why did someone move my question to Visual Studio WPF Designer when it has more to do with Blend than VS?
|
| azoapes Friday, May 16, 2008 3:07 PM |
OK, I misunderstood what you were trying to do. I thought you had problems editing your UserControl.xaml file with the designer as opposed to parenting into an instance of it in a different XAML file.
To do the latter, you can define your UserControl like you did in your first post but need to put a Grid into the instance of the UserControl so that it can accept children, e.g. like this:
Code Snippet
<uc:UserControl1>
<Grid>
</Grid>
</uc:UserControl1>
After this you will be able to drag controls into the Grid. |
| Marco Goertz - MSFT Monday, May 19, 2008 4:02 PM |
That is not a solution, it's a work-around. I don't want for meor let's saya customertoadd additional XAML every timethey place outmy UserControl,they want to just drag it out and then place things in it... not add a Grid inside every one of them. I want the UserControl to be the host. The customer may be a designer without knowledge of any XAML or code-behind.
The point of all this is automation, of course I could copy and paste every instance and I wouldn't even have to make a UserControl at all, but I want to know how to add design-time support for Blend to a Grid inside ofa UserControl.
Obviously the Grid control has this, as an example. |
| azoapes Monday, May 19, 2008 8:40 PM |
Do you mean you want to do the following:
- Create a UserControl
- Add it to Window1.xaml
- Drag and drop controls into the UserControl
|
| Mark Boulter Tuesday, May 20, 2008 3:25 PM |
Yes, exactly! Sorry if I was unclear, English is not my native language (and I use that excuse a lot ).
Regards from Sweden
/Simeon |
| azoapes Tuesday, May 20, 2008 4:11 PM |
Thanks, now I understand a bit more - in order to add things to containers you need to write add design time support for that container. Can you describe in a bit more detail what you are trying to do? Why are you starting with UserControl rather than ContentControl or Control or Panel for example?
mark |
| Mark Boulter Tuesday, May 20, 2008 5:32 PM |
| Mark Boulter wrote: |
|
Can you describe in a bit more detail what you are trying to do? Why are you starting with UserControl rather than ContentControl or Control or Panel for example?
| |
The answer to your question is that I honestly have no idea. I'm a beginner at WPF and just got the tip; use a UserControl and create a custom panel... got the tip to inherit the Grid classtoo, but that didn't work out well.
I suppose now you know a much better way to add design-time container support to a control? The control I'm buildingwillget pretty complex in the end, but right now I'm starting out with maybea rectangle and a Grid inside of it.
To clarify, here's a summary: I want a container and a rounded rect around it, and the control should auto-expand when someone add controls into its container. I know how to do a UserControl that looks like this, but not how I create a container inside of it that has design-time support. I also know some basics about writinga custom panel, maybe that'sthe bestway to do it?
Regards, Simeon |
| azoapes Tuesday, May 20, 2008 5:54 PM |
UserControl is really designed to be used as aclosed "form like" control - for example an Address control or a Customer control - rather than as the starting point for a new container. It has some funkiness around naming and namescopes that are likely to cause you problems.
Acustom panel would be a better way to go. If you start with Grid or ContentControl you'll get parenting and placement for free in the designer. If you start with your own custom panel you'll have to write some design time support. Additionally I think that the design time support will only work in Cider - I don't believe that Blend has support for custom panels but you should check that on the Blend forums rather than taking my word for it. Here is a simple subclass of ContentControl that works in Blend and Cider as a starting point. It works because both designers treat it as a ContentControl.
CustomControl1.cs:
public class CustomControl1 : ContentControl {
static CustomControl1() {
DefaultStyleKeyProperty.OverrideMetadata( typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
Themes\Generic.xaml:
< ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication225">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ContentControl}">
<Grid>
<Rectangle Fill="#FF3C3C3C" RadiusX="15" RadiusY="15" x:Name="rectangle">
</Rectangle>
<ContentPresenter/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ ResourceDictionary>
|
| Mark Boulter Tuesday, May 20, 2008 6:06 PM |
Don't see any further responses on this so not sure if this is resolved or not.
I think there is a basic understanding issue. UserControls are more meant to be reusable controls on their own. As a simple example, you can treate existing controls like TextBox, Button, ListView etc are usercontrols. You can create your own, but then usually they are self contained and when dropped on a Window.xaml, they are meant to be already complete.
For what you are trying to do, you need a container control and not a usercontrol. There are existing container controls like Grid, Canvas, StackPanel, WrapPanel and DockPanel that should address pretty much most of your basic needs for layout. MVP Client App |
| Atul Gupta Thursday, June 05, 2008 7:58 AM |