WPF 数据绑定到分层数据模板中的父属性

本文关键字:属性 数据绑定 分层 数据 WPF | 更新日期: 2023-09-27 18:34:47

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

        <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返回到父对象以访问其属性呢?

WPF 数据绑定到分层数据模板中的父属性

归咎于疲劳...答案很简单:

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