2013-12-14 35 views
1

我有一個ListView的DataTemplate裏面的RichTextBox控件。這個想法是,我想動態地添加運行/ InlineUIElements /圖像等等在列表視圖中的綁定時間的富文本框。問題是沒有關於數據綁定或類似事件。我嘗試了RichTextBox的Loaded事件,但看起來WPF重用了控件,因此當我滾動時(錯誤順序放置錯誤的內容,因爲負載事件在滾動期間觸發),內容被搞亂了。我還應該提到通過手動將行添加到ListView.Items集合,在代碼隱藏中發生了與ListView的綁定。WPF/Windows 8應用程序DataTemplate Databind事件

相關標記

<ListView Background="#F7F7F7" HorizontalAlignment="Stretch" Foreground="Black" x:Name="chatPane" Grid.Row="1" 
        ScrollViewer.HorizontalScrollBarVisibility="Disabled" HorizontalContentAlignment="Stretch" SelectionMode="Multiple" 
       ItemTemplateSelector="{StaticResource messageTypeDataTemplateSelector}" SelectionChanged="ChatPane_OnSelectionChanged"> 
     </ListView> 

<common:MessageTypeDataTemplateSelector 
     TextMessageTemplate="{StaticResource TextMessage}" 
     EnterMessageTemplate="{StaticResource EnterMessage}" 
     ExitMessageTemplate="{StaticResource ExitMessage}" 
     TimestampMessageTemplate="{StaticResource TimestampMessage}" 
     ImageMessageTemplate="{StaticResource ImageMessage}" 
     x:Key="messageTypeDataTemplateSelector" /> 

<DataTemplate x:Key="TextMessage"> 
     <Grid Grid.ColumnSpan="3" RowSpan="1"> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="Auto"/> 
       <ColumnDefinition Width=".5*"/> 
       <ColumnDefinition Width="70*"/> 
      </Grid.ColumnDefinitions> 
      <TextBlock Text="{Binding UserName}" Visibility="{Binding Source={StaticResource chatSettings}, Path=HideAvatars, Converter={StaticResource booleanToVisibility}}" FontWeight="Bold" TextAlignment="Right" Grid.Column="0" Width="150" /> 
      <Image VerticalAlignment="Top" Source="{Binding AvatarUrl}" Visibility="{Binding Source={StaticResource chatSettings}, Path=ShowAvatars, Converter={StaticResource booleanToVisibility}}" Grid.Column="0" Width="60" Margin="0,0,10,0" /> 
      <TextBlock Text=" : " Visibility="{Binding Source={StaticResource chatSettings}, Path=HideAvatars, Converter={StaticResource booleanToVisibility}}" Grid.Column="1" /> 
      <RichTextBlock Loaded="FrameworkElement_OnLoaded" TextWrapping="Wrap" Grid.Column="2" /> 
     </Grid> 
    </DataTemplate> 
+0

就這麼你知道,Windows 8商店應用程序不使用WPF。他們正在使用Windows運行時(RT),它不是WPF而不是Silverlight,儘管它們之間有一些相似之處。我知道這不會回答你的問題,但只是分享,因爲它可以在瀏覽MSDN文檔時派上用場。 –

+0

你的FrameworkElement_OnLoaded方法是什麼樣的? –

回答

4

你是絕對正確的。 WinRT中沒有OnDataBinding事件。這個想法如何:

爲RichTextBlock創建一個AttachedProperty(http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx)然後綁定您的項目。當您註冊附加屬性時,您可以在FrameworkPropertyMetadata中指定一個PropertyChangedCallback,只要附加屬性的值發生變化,該屬性就會觸發。就像這樣:

現在,以確保它的工作原理,在當前的.xaml.cs文件執行以下操作:

public static readonly DependencyProperty RichTextBlockItemProperty = DependencyProperty.RegisterAttached(
    "RichTextBlockItem", 
    typeof(object), 
    typeof(RichTextBlock), 
    new PropertyMetadata(null, RichTextBlockItemChanged) 
); 

// Don't forget this! 
public static object GetRichTextBlockItem(DependencyObject obj) 
{ 
    return (object)obj.GetValue(RichTextBlockItemProperty); 
} 

// And this! 
public static void SetRichTextBlockItem(DependencyObject obj, object value) 
{ 
    obj.SetValue(RichTextBlockItemProperty, value); 
} 

public void RichTextBlockItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
{ 
    // Here you can do whatever you wish. This method will fire when the item is bound. 
    // d is the RichTextBlock object. 
    // e.NewValue is the value that was just bound. 
    // So, from here you can dynamically add your Runs/Images/etc 
} 

然後在您的.xaml文件確保你添加本地命名空間,以便你可以這樣做:

<Page .... xmlns:local="using:CurrentXamlPageNamespace".../> 

<DataTemplate x:Key="TextMessage"> 
    <Grid Grid.ColumnSpan="3" RowSpan="1"> 
     <!-- I got rid of the other xaml just to hightlight my answer. But you still need it. --> 
     <RichTextBlock local:RichTextBlock.RichTextBlockItem="{Binding}" TextWrapping="Wrap" Grid.Column="2" /> 
    </Grid> 
</DataTemplate> 
+0

嗯。我想我在這裏得到你想要做的,但是你能告訴我一些樣品XAML嗎?問題是並非所有綁定到此ListView的項目都將具有RichTextBox。這就是爲什麼我有爵士樂自定義數據模板。另外,上面的代碼到底在哪裏?在我工作的.xaml.cs頁面文件中? – Richthofen

+0

好吧,我會更新我的代碼,但如果您有其他沒有RichTextBlocks的模板,那麼您可能會創建一個更一般的依賴屬性,您可以將它綁定到所有模板中。 –

+0

嗯,Windows運行時支持FrameworkPropertyMetadata?無法找到類型或名稱空間名稱「FrameworkPropertyMetadata」(您是否缺少使用指令或程序集引用?) – Richthofen

相關問題