C# WPF/Windows 8应用程序数据模板数据绑定事件

C# WPF/Windows 8应用程序数据模板数据绑定事件,c#,windows-8,windows-runtime,winrt-xaml,C#,Windows 8,Windows Runtime,Winrt Xaml,我在ListView的DataTemplate中有一个RichTextBox控件。其思想是,我希望在绑定时将Runs/InlineUIElements/images等动态添加到Listview中的富文本框中。问题是没有ondatabinding或类似事件。我尝试了RichTextBox的Loaded事件,但似乎WPF重用了控件,因此在我滚动时内容被弄乱了(将错误的内容按错误的顺序放置,因为load事件是在滚动期间触发的)。我还应该提到,通过手动向ListView.Items集合添加行,到List

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

相关标记

 <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()创建AttachedProperty,然后将项目绑定到该属性。注册附加属性时,可以在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>

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

为RichTextBlock()创建AttachedProperty,然后将项目绑定到该属性。注册附加属性时,可以在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>


正如您所知,Windows 8应用商店应用程序不使用WPF。他们使用的是Windows运行时(RT),它不是WPF,也不是Silverlight,尽管两者有一些相似之处。我知道这并不能回答你的问题,但只是分享,因为它在查看MSDN文档时很方便。你的FrameworkElement_onload方法是什么样子的?正如你所知,Windows 8应用商店应用程序不使用WPF。他们使用的是Windows运行时(RT),它不是WPF,也不是Silverlight,尽管两者有一些相似之处。我知道这并不能回答你的问题,但只是分享,因为它在查看MSDN文档时很方便。你的FrameworkElement_OnLoaded方法是什么样子的?嗯。我想我知道你在这里想做什么,但是你能给我看一些XAML示例吗?问题是,并非所有绑定到此ListView的项都将具有RichTextBox。这就是为什么我有自定义数据模板jazz。另外,上面的代码具体在哪里?在我正在使用的.xaml.cs页面文件中?好的,我会更新我的代码,但是如果您有其他没有RichTextBlocks的模板,那么您可以创建一个更通用的依赖属性,您可以在所有模板中绑定到该属性。嗯,Windows运行时支持FrameworkPropertyMetadata吗?找不到类型或命名空间名称“FrameworkPropertyMetadata”(是否缺少using指令或程序集引用?)。PropertyMetadata.让我们来看看,嗯,我想我明白你在这里想做什么,但是你能给我看一些XAML样本吗?问题是,并非所有绑定到此ListView的项都将具有RichTextBox。这就是为什么我有自定义数据模板jazz。另外,上面的代码具体在哪里?在我正在使用的.xaml.cs页面文件中?好的,我会更新我的代码,但是如果您有其他没有RichTextBlocks的模板,那么您可以创建一个更通用的依赖属性,您可以在所有模板中绑定到该属性。嗯,Windows运行时支持FrameworkPropertyMetadata吗?找不到类型或命名空间名称“FrameworkPropertyMetadata”(是否缺少using指令或程序集引用?)。让我们来看看