简体   繁体   English

与父财产WPF绑定

[英]Binding with parent proprety WPF

here is my code: (explanation of my problem after) 这是我的代码:(之后我的问题的解释)

<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>

Ok, so my first eventtrigger does not work. 好的,所以我的第一个eventtrigger不起作用。 I'm trying to send the value of my textblock named "Name"(just above). 我正在尝试发送名为“名称”(正上方)的文本块的值。 But he doesn't find this name. 但是他找不到这个名字。 So he send me a null value. 所以他给我寄了一个空值。

I know that the problem is comming from the lign <TextBlock x:Name="File_name" Text="{Binding}"> I'm changing my datacontext so i'd like to be in the parent datacontext to get property "Text" of the texblock named "Name". 我知道问题出在<TextBlock x:Name="File_name" Text="{Binding}">这是我要更改的数据上下文,所以我想在父datacontext中获取属性“ Text”名为“名称”的texblock的名称。

Rather than using ElementName in your binding, why not use ReleativeSource ? 为什么不使用ReleativeSource而不是在绑定中使用ElementName Use FindAncestor to get the TreeView and bind to the value on it's DataContext . 使用FindAncestor获取TreeView并绑定到其DataContext上的值。

Command="{Binding Path=DataContext.Current_playlist, ElementName=playlist}"

becomes

Command="{Binding Path=DataContext.Current_playlist, RelativeSource={RelativeSource FindAncestor, AncestorType=TreeView}}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM