Доступ к свойству, вызываемому внутри 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. Но возможно ли иметь доступ к свойству Description внутри этого ItemTemplate?


person imj    schedule 13.07.2018    source источник


Ответы (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>
person Babbillumpa    schedule 13.07.2018