簡體   English   中英

WPF:如何在屬性值更改時再次觸發觸發器?

[英]WPF: How to make a Trigger fire again when its property's value changes?

WPF和XAML新手,所以關於觸發器如何工作,我可能完全錯了……

基於這個問題 ,我使觸發器得以工作,但是它在屏幕的初始顯示中僅運行一次,並且接受該屬性的初始值是多少。 如果屬性稍后發生更改(僅在關閉該屏幕時可能發生),當我重新打開該屏幕時,觸發器的行為就好像該屬性沒有更改(即它使用該屬性的原始值)一樣。

可能使問題復雜化(也許是?)的是,定義觸發器的DependencyProperty的類是單例。 在我接觸代碼之前,這是一個單身人士。 我只是添加了DependencyProperty,所​​以可以在XAML代碼中添加樣式觸發器,以根據所選的報告類型獲得不同的行為。 在C#類屬性的getter / setter中,我必須添加“ .Instance ”來訪問GetValue()和SetValue(),因為該類是單例,並且我將C#屬性.Instance靜態。 不知道這是否會弄亂DependencyProperty方案,但是我知道只有一個ReportSettingsData對象被創建,因為構造函數中的Exception從未拋出。

這是單例課程的一部分:

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文本,而不管顯示報表屏幕時SelectedReport屬性的實際值如何。 但是,如果我將默認值更改為EquipSummary (或任何其他報告類型), HotPink為我提供所有四種報告類型的 HotPink文本。 如果SelectedReport屬性發生更改,如何使樣式觸發器和設置器重新運行?

嘗試如下更新現有代碼:

        <DataTrigger Binding="{Binding my:ReportSettingsData.SelectedReport}" Value="{x:Static my:SelectedReportType.DiagSummary}">
            <Setter Property="TextBlock.Foreground" Value="{StaticResource BgBrush1}" />
        </DataTrigger>

另外,您綁定到DataGrid Item Source的內容也請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM