2

I'm trying to create a UserControl which is a legend of a graph item, I've defined it so it has a label with the graph name, a check box which define whether we show it or not and a rectangle with the graph color.

The xaml is defined like this:

<UserControl x:Class="Resources.LegendItem" 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"> <UserControl.Template> <ControlTemplate> <StackPanel Margin="1,0,0,0" Orientation="Horizontal"> <CheckBox Name="cb" IsChecked="{TemplateBinding IsGraphVisible}" > <StackPanel Margin="1,0,0,0" Orientation="Horizontal"> <Rectangle Name="rec" RadiusX="2" RadiusY="2" Height="10" Width="10" /> <Label Name="lab" Content="{TemplateBinding GraphName}" /> </StackPanel> </CheckBox> </StackPanel> </ControlTemplate> </UserControl.Template> </UserControl> 

and the cs file is:

namespace Resources { public partial class LegendItem : UserControl { public static readonly DependencyProperty IsGraphVisibleProperty = DependencyProperty.Register("IsGraphVisible", typeof(Boolean), typeof(LegendItem)); public static readonly DependencyProperty GraphNameProperty = DependencyProperty.Register("GraphName", typeof(String), typeof(LegendItem)); public bool IsGraphVisible { get { return (bool)GetValue(IsGraphVisibleProperty); } set { SetValue(IsGraphVisibleProperty, value); } } public string GraphName { get { return (string)GetValue(GraphNameProperty); } set { SetValue(GraphNameProperty, value); } } public LegendItem() { InitializeComponent(); } } } 

But when I compile it, I get an error "Cannot find the static member 'IsGraphVisibleProperty' on the type 'Control'." Any help would be appreciated.

2 Answers 2

2

You do not need a template at all. UserControl allows the XAML to be declared directly. You can't set the template in a UserControl:

<UserControl x:Class="Resources.LegendItem" x:Name="MyControl" 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"> <StackPanel Margin="1,0,0,0" Orientation="Horizontal"> <CheckBox Name="cb" IsChecked="{Binding ElementName=MyControl, Path=IsGraphVisible}" > <StackPanel Margin="1,0,0,0" Orientation="Horizontal"> <Rectangle Name="rec" RadiusX="2" RadiusY="2" Height="10" Width="10" /> <Label Name="lab" Content="{Binding ElementName=MyControl, Path=GraphName}" /> </StackPanel> </CheckBox> </StackPanel> </UserControl> 
Sign up to request clarification or add additional context in comments.

Comments

1

You need to specify TargetType="{x:Type Resources.LegendItem}" on your ControlTemplate, otherwise it defaults to being a template for Control and you get that error.

3 Comments

By the way, why is it a template for Control instead of UserControl?
Because Control is the most general control that can be templated. The Template property first appears in Control.
UserControls do not allow the template to be set.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.