对ItemTemplate内部调用的属性的访问,而不是对其绑定的访问



具有此属性和绑定到其中一个的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的属性?

您可以使用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>

最新更新