WPF and XAML newbie here....
I need to tie a WPF Trigger
or DataTrigger
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 Trigger
or DataTrigger
that involves properties that already exist (e.g. MouseOver
), none of them give examples of how to tie it in with your own C# code.
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 TextBlock.TextAlignment="Left"
, while all other reports (i.e. the default) should be TextBlock.TextAlignment="Center"
. (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.
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...
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));
}
}
So in my XAML file, I've played with various incantations of Trigger
and DataTrigger
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.
<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">
<DataGrid Name="_dgReport"
ColumnWidth="Auto"
CanUserAddRows="False"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto"
ItemsSource="{Binding}"
IsReadOnly="True">
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center"></Setter>
<Style.Triggers>
<!-- Override some property settings for Diagnostics reports... -->
<!--
<DataTrigger Binding="{Binding my:ReportSettingsData.SelectedReport}" Value="DiagSummary">
<DataTrigger Binding="{Binding Path=my:ReportSettingsData.SelectedReport}" Value="DiagSummary">
-->
<Trigger Property="my:ReportSettingsData.SelectedReport" Value="DiagSummary">
<Setter Property="TextBlock.TextAlignment" Value="Left"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
</DataGrid>
</my:HeaderVisual>
How can I get my Trigger
to fire when ReportSettingsData.SelectedReport == SelectedReportType.DiagSummary
?