Skip to main content
 首页 » 编程设计

c#之WPF/Windows 8 应用程序 DataTemplate 数据绑定(bind)事件

2024年02月13日14me-sa

我在 ListView 的 DataTemplate 中有一个 RichTextBox 控件。我的想法是,我想在绑定(bind)时将 Runs/InlineUIElements/images 等动态添加到 Listview 中的富文本框。问题是没有 ondatabinding 或类似事件。我尝试了 RichTextBox 的 Loaded 事件,但似乎 WPF 重用了控件,因此当我滚动时内容被弄乱了(以错误的顺序放置错误的内容,因为加载事件在滚动期间触发)。我还应该提到,通过手动将行添加到 ListView.Items 集合,绑定(bind)到 ListView 是在代码隐藏中进行的。

相关标记

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

请您参考如下方法:

你完全正确。 WinRT 中没有 OnDataBinding 事件。这个想法怎么样:

为 RichTextBlock (http://msdn.microsoft.com/en-us/library/ms749011(v=vs.110).aspx) 创建一个 AttachedProperty,然后将您的项目绑定(bind)到它。当您注册附加属性时,在 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>