C
C#2y ago
Anton

WPF custom property not recognized by the compiler

I have defined a custom property like below, it's in the namespace CarApp. It is supposed to represent the binding source that I would "pass" to a control template, defined as a static resource, by it grabbing it from the parent.
public static class TemplateBinding
{
public static readonly DependencyProperty Property = DependencyProperty.RegisterAttached(
typeof(TemplateBinding).Name, ownerType: typeof(DataGridColumn), propertyType: typeof(string));

public static string Get(DataGridColumn target)
{
return (string) target.GetValue(Property);
}

public static void Set(DataGridColumn target, string value)
{
target.SetValue(Property, value);
}
}
public static class TemplateBinding
{
public static readonly DependencyProperty Property = DependencyProperty.RegisterAttached(
typeof(TemplateBinding).Name, ownerType: typeof(DataGridColumn), propertyType: typeof(string));

public static string Get(DataGridColumn target)
{
return (string) target.GetValue(Property);
}

public static void Set(DataGridColumn target, string value)
{
target.SetValue(Property, value);
}
}
Then, at the top of my xaml component file, I define a local namescope like this xmlns:local="clr-namespace:CarApp". Then, further on, I define my control template, and then my DataGridTemplateColumn like this:
<Grid.Resources>
<DataTemplate x:Key="datePicker">
<DatePicker SelectedDate="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=(local:TemplateBinding.Property)}"/>
</DataTemplate>
</Grid.Resources>

<!-- ... -->

<DataGrid.Columns>
<DataGridTemplateColumn
Header="ManufacturedOn"
CellTemplate="{StaticResource datePicker}"
local:TemplateBinding.Property="ManufacturedDate" />
</DataGrid.Columns>
<Grid.Resources>
<DataTemplate x:Key="datePicker">
<DatePicker SelectedDate="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=(local:TemplateBinding.Property)}"/>
</DataTemplate>
</Grid.Resources>

<!-- ... -->

<DataGrid.Columns>
<DataGridTemplateColumn
Header="ManufacturedOn"
CellTemplate="{StaticResource datePicker}"
local:TemplateBinding.Property="ManufacturedDate" />
</DataGrid.Columns>
When I try to compile the app, there's an error:
error MC3072: The property 'TemplateBinding.Property' does not exist in XML namespace 'clr-namespace:CarApp'. Line 59 Position 21.
error MC3072: The property 'TemplateBinding.Property' does not exist in XML namespace 'clr-namespace:CarApp'. Line 59 Position 21.
Which points to the line where I apply the property. Clearing the temp files (obj) does not help. What's weird tho, is that removing the App.xaml (the entry point) and then compiling doesn't compain about that, it now says that there's no entry point, and the IDE doesn't show me an error in the xaml file, so I guess it compiled, but there might be error order at play too.
8 Replies
sibber
sibber2y ago
i think you have to name them GetTemplateBinding SetTemplateBinding TemplateBindingProperty since it probably uses reflection to find and invoke these methods also nameof(TemplateBinding) is a const and returns the same thing as typeof(TemplateBinding).Name
Anton
Anton2y ago
ah then SetProperty GetProperty would work too
sibber
sibber2y ago
Set<RegisteredPropertyName> etc which in your case is typeof(TemplateBinding).Name
Anton
Anton2y ago
I know, I just used that in a generic base class that got the derived type via a generic parameter, so I couldn't use that right gotcha
sibber
sibber2y ago
Follow the WPF property naming convention that distinguishes fields from the properties that they represent, by naming the identifier field <property name>Property. Also, provide static Get<property name> and Set<property name> accessor methods, which lets the property system access your attached property.
sibber
sibber2y ago
Attached properties overview - WPF .NET
Learn about the WPF property system and the capabilities of an attached property, which are global properties settable on any object.
sibber
sibber2y ago
i think visual studio has a code snippet for attached properties yep propa