C
C#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.
S
sibber558d 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
A
Anton558d ago
ah then SetProperty GetProperty would work too
S
sibber558d ago
Set<RegisteredPropertyName> etc which in your case is typeof(TemplateBinding).Name
A
Anton558d 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
S
sibber558d 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.
S
sibber558d 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.
S
sibber558d ago
i think visual studio has a code snippet for attached properties yep propa
Want results from more Discord servers?
Add your server
More Posts
Entity Framework DbContextOptionsBuilder.UseSqlServer() missing definitionI just installed the newest version of Entity Framework (6.0.9) and am inside my program.cs trying tDoes API manual nuget downloads come with the package dependencies?-WPF passing binding to a template from DataGridTemplateColumnSay, I have a template like this in resources, which I want to reuse for multiple columns, and whichBest way to implement a regex based lexer [Answered]In a regex lexer, you can loop over every pattern and do something like this: ```cs if (match.Successtring query in C Sharpi am trying to get values from 2 website links and assign them to a lable using visual studio one is there a convention for validating login sessions with MVC?Assuming I'm using ASP, ADO and .NET Say I want to construct a project to have several functions thUsing SQL db to create object ID or application?I have an application where my records in my DB need to have unique IDs. Should I leave this responsObject Initializers - To Parenthesize or Not To Parenthesize (that is the question)What is the difference between the object initializers on lines 1 and 2? In what situations should oHow do I move my mouse curser on screen?I went through most of stack overflow and some YouTube tutorials but didn't fins anythingHow do I move my mouse curser on screen?i was wondering if its possible to move the curse on the screen using c#SQL Error when trying to get an IDI'm trying to add username password and email to my users table And then get the ID of this new userEF Structuring ProblemBasically I have users that I want to assign to a group. But I also want the group to have a parent,data transfer between classesI want to list 'uname' value in method X, to method Y i am new to oopA generic and efficient way to feed in text for a lexer or scannerIn the past I was lazy and always just shoved a string into my lexical analyzer as input, but what'sBlazor Re-render [SOLVED]Hello, I have a property stored in static class, and I would like to re-render some components that Restarting audio with MediaPlayer?I've made a button that uses MediaPlayer to play a custom MP3 file that I've made. ```cs MediaPlayerWhat's the meaning of CS0659?```'class' overrides Object.Equals(object o) but does not override Object.GetHashCode()``` I got thiHow does Newtonsoft.Json.JsonConverter handle nullable structs?I'm presently writing a JsonConverter for a third-party struct, and in my json models that field canMost convenient method to handle 3D vectors?I'm looking for a struct/class in which magnitude(L2 norm), normalize, multiplication with a scalar,can anybody recommend where I can learn about file format encoding internals e.g pdf, doc, epubscan anybody recommend where I can learn about file format encoding internals e.g pdf, doc, epubs