title: Using attached properties in Style Setter in WinUI 3
body: <p>I'm using WinUI 3 (1.1.3) in a desktop project with .NET 6. I'd like to be able to add an attached property to some of my Style Setters to set up Tracing. The idea is to use changes in the attached property to set up whatever type of Tracing I need for the specific property. Searching SO and the web gave me very little beyond some hints that the attached property setter doesn't get called by XAML when the Setter is processed.</p>
  <p>Here is my sample code for a <code>TraceHelper</code></p>
  <pre><code>    public static class TraceHelper
      {
          #region TraceThis
          // Set to True to trace changes in the property
          // Add to a Setter to trace the Property
          // &lt;Setter Property=&quot;PaneToggleButtonStyle&quot; Value=&quot;{StaticResource PaneToggleButtonStyle}&quot; local:TraceHelper.TraceThis=&quot;Trace&quot; /&gt;
      
          public static readonly DependencyProperty TraceThisProperty =
              DependencyProperty.RegisterAttached(&quot;TraceThis&quot;, typeof(string), typeof(TraceHelper), new PropertyMetadata(null, TraceThisChanged));
      
          public static string GetTraceThis(DependencyObject obj) =&gt; (string)obj.GetValue(TraceThisProperty);
      
          public static void SetTraceThis(DependencyObject obj, string value) =&gt; obj.SetValue(TraceThisProperty, value);
      
          private static void TraceThisChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
          {
              if (e.NewValue is string traceType)
              {
                  if (e.Property != TraceThisProperty) throw new System.Exception($&quot;TraceThis: Unknown target ({nameof(e.Property)}&quot;);
                  // deal with messages by type
                  switch (traceType)
                  {
                      case &quot;Announce&quot;:        // send simple Trace
                          My_TraceListener.TraceMessage(&quot;Setter&quot;, ((FrameworkElement)obj).Name, traceType);
                          break;
                      case &quot;Binding&quot;:         // send info about any binding
                          break;
                      case &quot;Trace&quot;:           // Trace changes in property
                          break;
                      case &quot;Value&quot;:           // send info about value assigned (including resolved resources)
                          break;
                      default: throw new System.Exception($&quot;TraceThis: Unknown trace type ({traceType}&quot;);
                  }
      
                  // Do whatever else needs doing
              }
          }
          #endregion
      }
  </code></pre>
  <p>This should (at least, I envision it will) be called from XAML like this</p>
  <pre><code>   &lt;!-- NavigationView --&gt;
      &lt;Style TargetType=&quot;cls:My_NavigationView&quot; x:Key=&quot;My_NavigationView&quot; BasedOn=&quot;{StaticResource GenericNavigationView}&quot; &gt;
          &lt;Setter Property=&quot;local:StyleHelper.StyleRead&quot; Value=&quot;My_NavigationView&quot; /&gt;
          &lt;Setter Property=&quot;PaneToggleButtonStyle&quot; Value=&quot;{StaticResource My_PaneToggleButtonStyle}&quot; local:TraceHelper.TraceThis=&quot;Announce&quot; /&gt;
          &lt;Setter Property=&quot;local:BindingHelper.CompactPaneLengthBindingPath&quot; Value=&quot;CurrentCompactPaneLength&quot; /&gt;
          &lt;Setter Property=&quot;Template&quot; Value=&quot;{StaticResource My_NavigationViewTemplate}&quot; /&gt;
      &lt;/Style&gt;
  </code></pre>
  <p>The second <code>Setter</code> should set the attached <code>TraceThis</code> property triggering the <code>TraceThisChanged()</code> method which then sets up the required tracing (in theory). In practice, although everything compiles and loads correctly, and <code>TraceThisProperty</code> does get instantiated, <code>SetTraceThis()</code> and <code>TraceThisChanged()</code> never get called.</p>
  <p>The first <code>Setter</code> does something somewhat similar: it sets the <code>StyleHelper.StyleRead</code> attached variable (attached to the object that loaded the Style) and starts Tracing. The third <code>Setter</code> actually sets up a requested <code>Binding</code> in code using an attached property. Both work well, so at least part of the concept is sound.</p>
  <p>I expect there's an issue with just exactly what object <code>TraceThis</code> is attached to (if anything). How might I determine exactly what's wrong here (please tell me it's not a boneheaded typo!)?</p>
  <p>Any ideas for an alternate way to accomplish this? Thanks for any input!</p>