вот мой код: (объяснение моей проблемы после)
<TreeView x:Name="playlist" Grid.Row="1" ItemsSource="{Binding Playlist}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding file}">
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="Names" Text="{Binding Name}" Margin="0,0,10,0"/>
<Button Content="add" Command="{Binding Path=DataContext.Add_file, ElementName=playlist}"
CommandParameter="{Binding ElementName=Names, Path=Text}">
</Button>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="File_name" Text="{Binding}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Path=DataContext.Current_playlist, ElementName=playlist}"
CommandParameter="{Binding ElementName=Name,Path=Text}">
</i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding Path=DataContext.Lecture, ElementName=playlist}"
CommandParameter="{Binding ElementName=File_name,Path=Text}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Итак, мой первый eventtrigger не работает. Я пытаюсь отправить значение моего текстового блока с именем "Имя" (чуть выше). Но он не находит этого имени. Поэтому он послал мне нулевое значение.
Я знаю, что проблема возникает из lign <TextBlock x:Name="File_name" Text="{Binding}">
Я меняю свой файл datacontext, поэтому я хочу быть в родительском datacontext, чтобы получить свойство "Текст", тексблока с именем "Имя".
Вместо использования ElementName
в вашей привязке, почему бы не использовать ReleativeSource
? Используйте FindAncestor
чтобы получить TreeView
и привязать к нему значение DataContext
.
Command="{Binding Path=DataContext.Current_playlist, ElementName=playlist}"
становится
Command="{Binding Path=DataContext.Current_playlist, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"