2012-09-28 65 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)。