2012-09-28 69 views
1

Iam在我的WPF應用程序中顯示消息
當消息添加新消息時,我需要突出顯示它。因此,我想動態獲取文本添加到文本塊在運行時在WPF ItemsControl的DataTemplate中訪問TextBlock的文本

我有這樣

<ItemsControl Name="DialogItemsControl" ItemsSource="{Binding Messages, Mode=OneWay}" Background="Transparent" 
          BorderBrush="Transparent" TargetUpdated="DialogItemsControl_TargetUpdated"> 
       <ItemsControl.ItemTemplate><!-- For ever message --> 
        <DataTemplate> 
         <Grid Margin="0,0,0,20"> 
          <ItemsControl Name="SubDialogItemsControl" 
            Foreground="{DynamicResource ButtonTextBrush}" 
            ItemsSource="{Binding Lines,NotifyOnTargetUpdated=True}" 
            Margin="0,0,0,12" 
            Grid.Column="0"> 
           <ItemsControl.ItemTemplate><!-- For every line --> 
            <DataTemplate> 
             <TextBlock Name="DialogMessageText" 
                Text="{Binding NotifyOnTargetUpdated=True}" 
              VerticalAlignment="Top" 
              Margin="0,2,0,2" 
              TextTrimming="WordEllipsis"/> 
            </DataTemplate> 
           </ItemsControl.ItemTemplate>          
          </ItemsControl> 
         </Grid> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 

,並在代碼隱藏類代碼中的XAML是這樣的:

private void DialogItemsControl_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e) 
     { 
      ItemsControl itemControl = sender as ItemsControl; 

      ContentPresenter dp = itemControl.ItemContainerGenerator.ContainerFromItem(itemControl.Items.CurrentItem) as ContentPresenter; 

      // Finding textBlock from the DataTemplate that is set on that ContentPresenter 
      DataTemplate myDataTemplate = dp.ContentTemplate; 
      ItemsControl itc = (ItemsControl)myDataTemplate.FindName("SubDialogItemsControl", dp); 
      if (itc != null && itc.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) 
      { 
       ContentPresenter cp = itc.ItemContainerGenerator.ContainerFromIndex(0) as ContentPresenter; 
       DataTemplate dt = cp.ContentTemplate; 
       TextBlock tb = dt.LoadContent() as TextBlock;    

       tb.TargetUpdated += new EventHandler<System.Windows.Data.DataTransferEventArgs>(myTextBlock_TargetUpdated); 
      }    
     } 

void myTextBlock_TargetUpdated(object sender, System.Windows.Data.DataTransferEventArgs e) 

     { 

      TextBlock tb = sender as TextBlock; 
      //When i access the text property of tb, its showing null, how to get the text 
     } 

當我訪問文本塊的目標更新事件中的文本塊的文本屬性時,它顯示爲null,如何閱讀文本。

在此先感謝

回答

0

您解決從錯誤的角度問題(可能在這個過程中添加一個內存泄漏,因爲我沒有看到你退訂事件)。

您需要創建一個自定義TextBlock,覆蓋Text屬性的元數據,以便在文本字符串更改(通過PropertyChangedCallback)時它將Background更改爲幾秒鐘。

然後在您的ItemsControl的DataTemplate中使用該自定義TextBlock。

編輯 - 我認爲其他人能夠需要所以這裏這個功能是一個工作示例:

public class CustomTextBlock : TextBlock 
    { 
     static CustomTextBlock() 
     { 
      TextProperty.OverrideMetadata(typeof(CustomTextBlock), new FrameworkPropertyMetadata(null, 
       new PropertyChangedCallback(
        (dpo, dpce) => 
        { 
         //Flash the background to yellow for 2 seconds 
         var myTxtblk = dpo as CustomTextBlock; 
         if (myTxtblk != null) 
         { 
          myTxtblk.Background = Brushes.Yellow; 
          Task.Factory.StartNew(
           () => 
           { 
            Thread.Sleep(2000); 
            Application.Current.Dispatcher.Invoke(
             new Action(() => 
             { 
              myTxtblk.Background = Brushes.Transparent; 
             })); 
           }); 
         } 
        }))); 
     } 
    } 

然後,你需要聲明的權利的xmlns命名空間中的XAML視圖,並使用它像一個常規的TextBlock:

<local:CustomTextBlock Text="{Binding MyDynamicText}"/> 

時MyDynamicText被修改它會閃爍黃色(前提是它引發的PropertyChanged)。