WPF数据绑定到HierarchicalDataTemplate中的父属性

时间:2016-02-16 00:37:16

标签: c# wpf

我的列表框中有以下数据模板:

        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border BorderBrush="#FF575757" BorderThickness="0,0,0,1">
                <Grid HorizontalAlignment="Stretch" Tag="{Binding}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Icon"/>
                        <ColumnDefinition Width="*" SharedSizeGroup="Name"/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Image x:Name="DeviceIcon" DataContext="{Binding Path=device}" Source="{Binding Path=StatusIcon}" Width="64" Height="64" Grid.Column="0" Grid.RowSpan="2" RenderOptions.BitmapScalingMode="HighQuality" Margin="3"></Image>
                        <StackPanel Grid.Column="1" >
                            <TextBlock x:Name="DeviceName" DataContext="{Binding Path=device}" Text="{Binding Path=DeviceName}" FontWeight="Bold" Foreground="#FF00008F" FontSize="14.667"/>
                            <TextBlock x:Name="PluginName" Text="{Binding Path=PluginName}" />
                        </StackPanel>
                        <Menu x:Name="MainMenu" VerticalAlignment="Top" Padding="0,3" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding deviceMenu}">
                        <Menu.ItemTemplate>
                            <HierarchicalDataTemplate ItemsSource="{Binding Items}">
                                <StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
                                    <Image Source="{Binding Icon}" Width="16" Height="16">
                                        <Image.Style>
                                            <Style TargetType="{x:Type Image}">
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding Icon}" Value="{x:Null}">
                                                        <Setter Property="Visibility" Value="Collapsed" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </Image.Style>
                                    </Image>
                                    <TextBlock Text="{Binding Text}"/>
                                </StackPanel>
                            </HierarchicalDataTemplate>

                        </Menu.ItemTemplate>
                        <Menu.ItemContainerStyle>
                            <Style TargetType="{x:Type MenuItem}">
                                <Setter Property="Command" Value="{Binding ClickCommand}" />
                                <Setter Property="CommandParameter" Value="{Binding device}" />
                            </Style>
                        </Menu.ItemContainerStyle>
                    </Menu>


                </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>

MenuItem绑定到根对象中名为deviceMenu的属性。但是根对象还包含一个名为device的属性,需要将其映射到CommandParameter属性。

那么如何将deviceMenu放回到父对象以访问其属性?

1 个答案:

答案 0 :(得分:0)

归咎于疲劳......答案非常简单:

<Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Grid}}, Path=DataContext.device}" />