WPF:如何在其属性的值发生变化时再次触发Trigger?

时间:2015-07-29 14:25:43

标签: c# wpf xaml

WPF& XAML新手在这里,所以我很可能对触发器的工作方式有一个完全错误的概念......

基于this question,我让我的触发器工作,但它在屏幕的初始显示时只运行一次,并且取任何属性的初始值。如果属性稍后更改(仅当该屏幕关闭时才会发生),当我重新打开该屏幕时,触发器的行为就好像属性没有改变(即它使用属性的原始值)。

可能使问题复杂化(可能?)是定义Trigger的DependencyProperty的类是单例。在我触摸代码之前它是一个单身人士;我刚刚添加了DependencyProperty,因此我可以在XAML代码中添加样式触发器,以根据所选的报告类型获得不同的行为。在C#类属性getter / setter中,我必须添加“.Instance”来访问GetValue()和SetValue(),因为该类是单例,我使C#属性成为静态。不确定是否会破坏DependencyProperty方案,但我知道只创建了一个ReportSettingsData对象,因为构造函数中的异常永远不会被抛出。

这是单身人士课程的一部分:

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
      {
         // This is a singleton; the constructor should be called only once. Set _instance here so that
         // it's available immediately in case the constructor needs to access any DependencyProperty's.
         if (_instance != null)
            throw new Exception("ReportSettingsData ctor was called twice.");
         _instance = this;
         // ...other unrelated constructor code...
      }

      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)

                     // Set the default state of the 'SelectedReport' property
                     new PropertyMetadata(SelectedReportType.DiagSummary));

);        }     }

报告页面的XAML:

<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">


            <!-- Default setters for ALL report types... -->
            <Setter Property="TextBlock.Foreground" Value="HotPink"></Setter>


            <!-- But override some settings for Diagnostics reports... -->
            <Style.Triggers>
               <Trigger Property="my:ReportSettingsData.SelectedReport"  Value="{x:Static my:SelectedReportType.DiagSummary}">
                  <Setter Property="TextBlock.Foreground" Value="Goldenrod"></Setter>
               </Trigger>
            </Style.Triggers>


         </Style>
      </DataGrid.Resources>
   </DataGrid>

</my:HeaderVisual>

SelectedReport属性的默认值在上面C#代码的最后一行中设置。如果我将默认值设置为DiagSummary,则会触发XAML代码中的触发器,并且所有四种报告类型Goldenrod文本,无论{{{>的实际值如何1}}属性在我显示报告屏幕的点。但是,如果我将默认值更改为SelectedReport(或任何其他报告类型),则会为所有四种报告类型提供EquipSummary文本。我怎样才能获得Style Trigger&amp;如果HotPink属性发生变化,Setter会重新运行吗?

1 个答案:

答案 0 :(得分:0)

尝试更新现有代码,如下所示:

<textarea placeholder="">Sed augue ipsum, egestas nec, vestibulum et, malesuada adipiscing, dui. Aenean commodo ligula eget dolor. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. Suspendisse faucibus, nunc et pellentesque egestas, lacus ante convallis tellus,
  vitae iaculis lacus elit id tortor. Curabitur blandit mollis lacus. Quisque ut nisi. Cras varius. Praesent ut ligula non mi varius sagittis.</textarea>

您对DataGrid项目来源的约束力也请告诉我。