title: WPF: How to make styles in XAML conditional on a variable in your own C# code
body: <p>WPF and XAML newbie here....</p>
  
  <p>I need to tie a WPF <code>Trigger</code> or <code>DataTrigger</code> in XAML code into some C# code in a class other than the class of the XAML control. This is very frustrating as all 28,000 tutorials I've read only give a trivial example for <code>Trigger</code> or <code>DataTrigger</code> that involves <em>properties that already exist</em> (e.g. <code>MouseOver</code>), none of them give examples of how to tie it in with your own C# code.</p>
  
  <p>I have a screen for displaying various report types. The XAML for all of the report types is the same, except that for diagnostic reports, my requirements are that the DataGrid cells be configured with <code>TextBlock.TextAlignment="Left"</code>, while all other reports (i.e. the default) should be <code>TextBlock.TextAlignment="Center"</code>. (There are a few other differences; for brevity I'll just say that's the only difference.) I really don't want to have to duplicate the entire XAML to special-case the diagnostics report, since 99% of it would be the same as the other reports.</p>
  
  <p>To use a Trigger, I thought perhaps I need my class to inherit from DependencyObject so I can define DependencyProperty's in it (being a WPF newbie I realize I may be saying some really outlandish things). So in my C# code, I have a class with this...</p>
  
  <pre><code>namespace MyApplication
  {
     public enum SelectedReportType
     {
        EquipSummary,
        EventSummary,
        UserSummary,
        DiagSummary
     }
  
     public sealed class ReportSettingsData : DependencyObject
     {
        private static ReportSettingsData _instance; // singleton
  
        static ReportSettingsData() { new ReportSettingsData(); }
  
        private ReportSettingsData() // private because it's a singleton
        {
           if (_instance == null) // only true when called via the static constructor
              _instance = this; // set here instead of the static constructor so it's available immediately
           SelectedReport = SelectedReportType.EquipSummary; // set the initial/default report type
        }
  
        public static ReportSettingsData Instance
        {
           get { return _instance; }
        }
  
        public static SelectedReportType SelectedReport
        {
           get { return (SelectedReportType)Instance.GetValue(SelectedReportProperty); }
           set { Instance.SetValue(SelectedReportProperty, value); }
        }
  
        public static readonly DependencyProperty SelectedReportProperty =
           DependencyProperty.Register("SelectedReport", typeof(SelectedReportType), typeof(ReportSettingsData));
     }
  }
  </code></pre>
  
  <p>So in my XAML file, I've played with various incantations of <code>Trigger</code> and <code>DataTrigger</code> and can't figure out how to make it work. In every case, the diagnostic report has the same default characteristics of the other reports.</p>
  
  <pre><code>&lt;my:HeaderVisual x:Class="MyApplication.ReportsView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:my="clr-namespace:MyApplication"&gt;
  
     &lt;DataGrid Name="_dgReport"
                  ColumnWidth="Auto"
                  CanUserAddRows="False"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Auto"
                  ItemsSource="{Binding}"
                  IsReadOnly="True"&gt;
        &lt;DataGrid.Resources&gt;
           &lt;Style TargetType="DataGridCell"&gt;
              &lt;Setter Property="TextBlock.TextAlignment" Value="Center"&gt;&lt;/Setter&gt;
              &lt;Style.Triggers&gt;
                 &lt;!-- Override some property settings for Diagnostics reports... --&gt;
                 &lt;!--
                    &lt;DataTrigger Binding="{Binding my:ReportSettingsData.SelectedReport}"  Value="DiagSummary"&gt;
                    &lt;DataTrigger Binding="{Binding Path=my:ReportSettingsData.SelectedReport}"  Value="DiagSummary"&gt;
                 --&gt;
                 &lt;Trigger Property="my:ReportSettingsData.SelectedReport"  Value="DiagSummary"&gt;
                    &lt;Setter Property="TextBlock.TextAlignment" Value="Left"&gt;&lt;/Setter&gt;
                 &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
           &lt;/Style&gt;
        &lt;/DataGrid.Resources&gt;
     &lt;/DataGrid&gt;
  
  &lt;/my:HeaderVisual&gt;
  </code></pre>
  
  <p>How can I get my <code>Trigger</code> to fire when <code>ReportSettingsData.SelectedReport == SelectedReportType.DiagSummary</code>?</p>