I’ve been using Sandcastle and Sandcastle Help File Builder (SHFB) recently to produce some SDK documentation and noticed that topics were automatically created for attached properties like the one shown here. But how do you actually populate these topics and how do you link back to them? Well let’s work through a simple example.
/// <summary> /// Identifies the MyAttached attached property. /// </summary> public static readonly DependencyProperty MyAttachedProperty = DependencyProperty.RegisterAttached("MyAttached", typeof(string), typeof(MyClass)); public static string GetMyAttached(DependencyObject element) { // Argument validation omitted. return (string)element.GetValue(MyAttachedProperty); } public static void SetMyAttached(DependencyObject element, string value) { // Argument validation omitted. element.SetValue(MyAttachedProperty, value); }
With attached properties there’s no physical property in the code for you to document. The only members that can be documented with the code are the dependency property field and the get/set helper methods. So how do you add documentation to the attached property topic? Well you can manually create an additional XML comment file for the assembly containing the attached property and add the following content:
<?xml version="1.0"?> <doc> <assembly> <name>MyTest</name> </assembly> <members> <member name="P:MyTest.MyClass.MyAttached"> <summary>Summary for MyAttached attached property.</summary> </member> </members> </doc>
Add the XML comment file to the Documentation Sources of your SHFB project and the summary comments will appear in the attached property topic when the project is rebuilt. But what about linking back to the attached property topic from the dependency property field’s summary comment? Unfortunately the C# compiler will warn about both of the following:
/// <summary> /// Identifies the <see cref="MyAttached" /> attached property. /// </summary> /// <summary> /// Identifies the <see cref="MyTest.MyClass.MyAttached" /> attached property. /// </summary>
In both cases the compiler warns that it cannot find a member matching the member specified in the see tag. This shouldn’t be that surprising because we already know there’s no physical property in the code. Sandcastle will therefore be unable to provide a link and the resulting text will appear as bold. Fortunately there is a way to provide a valid member reference for the attached property, in fact we’ve already done so in the separate XML comment file.
/// <summary> /// Identifies the <see cref="P:MyTest.MyClass.MyAttached" /> attached property. /// </summary>
Now the C# compiler is happy and Sandcastle can generate a link back to the attached property topic.