访问绑定之外的ItemTemplate内部的属性

时间:2018-07-13 07:18:54

标签: wpf vb.net binding itemssource

具有此属性以及与其中一个绑定的ItemsControl:

Public Property Description As String
    Get
        Return _description
    End Get
    Set(value As String)
        _description = value
    End Set
End Property

Public Property Options As List(Of ItemOption)
    Get
        Return _options
    End Get
    Set(value As List(Of ItemOption))
        _options = value
    End Set
End Property

<ItemsControl ItemsSource="{Binding Path=Options}" ItemTemplate="{DynamicResource OptionsTemplate}"/>

在ItemTemplate中,我当然可以访问名为Options的属性的属性。但是是否可以访问此ItemTemplate内名为Description的属性?

1 个答案:

答案 0 :(得分:1)

您可以使用 RelativeSource

例如,这就是您的 OptionTemplate

<UserControl x:Class="OptionTemplate"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:OptionTemplate"
             xmlns:i="clr-namespace:System.Windows.Interactivity;ssembly=System.Windows.Interactivity"
             xmlns:prism1="http://prismlibrary.com/"
             mc:Ignorable="d" >

   <Grid>
       <TextBlock Text="{Binding DataContext.Description, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}" />
       <!-- Your others controls -->
   </Grid>

</UserControl>