troubleshooting Question

Is there a way to create a WPF UserControl with an available grid in it?

Avatar of MarcoCastro
MarcoCastroFlag for Brazil asked on
Microsoft DevelopmentMicrosoft Applications.NET Programming
8 Comments1 Solution520 ViewsLast Modified:

   I need to create a WPF UserControl (or Control) that has two parts: a header with a Title and a grid right above it. The glitch is: I need to have access to this grid. I need to paste components in this grid. The component works right a Window with a header.

   My idea is create a constant part of the component (the header - the Title is a property) and variable part of it (the grid).

   Is is possible? How?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace McSoft.WPF.Controls
  /// <summary>
  /// Interaction logic for McBorder.xaml
  /// </summary>
  public partial class McBorderLookless : Control
    public McBorderLookless()
    private void SetupBindings()
      MultiBinding binding = new MultiBinding();
      Binding titleBinding = new Binding("Title");
      titleBinding.Source = this;
      titleBinding.Mode = BindingMode.TwoWay;
      Binding gridBinding = new Binding("BorderGrid");
      gridBinding.Source = this;
      gridBinding.Mode = BindingMode.TwoWay;
    static McBorderLookless()
        new FrameworkPropertyMetadata(typeof(McBorderLookless)));
      TitleProperty = DependencyProperty.Register("Title", typeof(string),
                new FrameworkPropertyMetadata("Type title...", new PropertyChangedCallback(OnTitleChanged)));
      GridProperty = DependencyProperty.Register("BorderGrid", typeof(Grid),
                new FrameworkPropertyMetadata(new PropertyChangedCallback(OnGridChanged)));
    // Tratando o título do border
    public static DependencyProperty TitleProperty;
    public string Title
      get { return (string)GetValue(TitleProperty); }
      set { SetValue(TitleProperty, value); }
    private static void OnTitleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
      McBorderLookless mcBorder = (McBorderLookless)sender;
      string newValue = (string)e.NewValue;
      mcBorder.Title = newValue.ToString();
    // Tratando o grid do border
    public static DependencyProperty GridProperty;
    public Grid BorderGrid
      get { return (Grid)GetValue(GridProperty); }
      set { SetValue(GridProperty, value); }
    private static void OnGridChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
      McBorderLookless mcBorder = (McBorderLookless)sender;
      Grid newValue = (Grid)e.NewValue;
      mcBorder.BorderGrid = newValue;
<ResourceDictionary xmlns=""
	<Style TargetType="{x:Type local:McBorderLookless}">
		<Setter Property="Template">
				<ControlTemplate TargetType="{x:Type local:McBorderLookless}">
					<Border CornerRadius="8" BorderThickness="2" BorderBrush="Gray" Grid.RowSpan="3">
								<RowDefinition Height="36" />
								<RowDefinition Height="225*" />
								<RowDefinition Height="36" />
							<Label Name="lblTitle" HorizontalAlignment="Left" Margin="6" FontWeight="Bold" 
								VerticalAlignment="Stretch" Content="{Binding Path=Title,RelativeSource={RelativeSource TemplateParent}}"></Label>
							<Path Stretch="Fill" Margin="4,0,4,4" VerticalAlignment="Bottom" Height="2" 
								Data="M4,30 L273,30" StrokeThickness="2" Fill="{x:Null}">
									<LinearGradientBrush EndPoint="0.738,0.24" StartPoint="0.201,0.25">
										<GradientStop Color="#FF000000" Offset="0"/>
										<GradientStop Color="#FFF9F9F9" Offset="1"/>
							<Grid Name="VoidGrid" Margin="0,0,0,0" Grid.Row="1" />

Open in new window

Jaime Olivares
Software Architect

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2008

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 8 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 8 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros