WPF ListView数据项右键菜单ContextMenu重写,以及右键菜单中button绑定ViewModel命令

想给数据项添加右键菜单,重新给右键菜单写了样式,右键菜单中包含button,如何给Button的Command绑定到页面的ViewModel,以下是代码示例

复制代码
        <DataTemplate x:Key="GridViewItemDataTemplate">
            <Border Width="80" Height="80" BorderBrush="Black" BorderThickness="1" Background="Black">
                <Border.ContextMenu>
                    <ContextMenu Width="88" Height="72">
                        <ContextMenu.Style>
                            <Style TargetType="{x:Type ContextMenu}">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type ContextMenu}">
                                            <Border Margin="0" Background="#FF171F27" BorderBrush="#FF707070" BorderThickness="1,1,1,1" CornerRadius="4">
                                                <Grid Margin="5,5,5,5">
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*"/>
                                                        <RowDefinition Height="*"/>
                                                    </Grid.RowDefinitions>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="20"/>
                                                        <ColumnDefinition Width="*"/>
                                                    </Grid.ColumnDefinitions>
                                                    <!--<Button Grid.Row="0" Grid.Column="0" Content="R+" 
                                                            Command="{Binding DataContext.UpdateAiCellCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Page}}"
                                                            CommandParameter="{Binding }"/>-->
                                                    <TextBlock Grid.Row="0" Grid.Column="0" Text="R:" Foreground="#FFFF0000" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
                                                    <uc:NumUpDown Grid.Row="0" Grid.Column="1" Value="{Binding RNum, Mode=TwoWay}" Foreground="#FFFF0000" FontSize="14" Height="25" VerticalAlignment="Center" CornerRadius="4"
                                                                  Minimum="0" Maximum="99" AllowInputType="AllowNumericOnly"/>
                                                    <TextBlock Grid.Row="1" Grid.Column="0" Text="G:" Foreground="#FF00FA00" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="14"/>
                                                    <uc:NumUpDown Grid.Row="1" Grid.Column="1" Value="{Binding GNum, Mode=TwoWay}" Foreground="#FF00FA00" FontSize="14" Height="25" VerticalAlignment="Center" CornerRadius="4"
                                                                  Minimum="0" Maximum="99" AllowInputType="AllowNumericOnly"/>
                                                </Grid>
                                            </Border>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </ContextMenu.Style>
                    </ContextMenu>
                </Border.ContextMenu>
                
                <Grid>
                    <Image Source="/Resources/Images/cell.png" Stretch="Fill"/>
                    <Grid Margin="5,8">
                        <Grid.Effect>
                            <DropShadowEffect Color="Black" BlurRadius="5" Direction="0" ShadowDepth="0" Opacity="1"  />
                        </Grid.Effect>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="17"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="17"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="10"/>
                            <ColumnDefinition Width="auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding RNum}" Foreground="#FFFF0000"/>
                        <TextBlock Grid.Row="0" Grid.Column="2" Text="{Binding GNum}" Foreground="#FF00FA00"/>
                        <TextBlock Grid.Row="2" Grid.Column="0" Text="{Binding CNum}" Foreground="#FF00FFFD"/>
                        <TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding YellowNum}" Foreground="#FFF7F800"/>
                    </Grid>
                    <CheckBox Width="16" Height="16" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,3,3"
                              IsChecked="{Binding IsChecked, Mode=TwoWay}">
                        <CheckBox.Effect>
                            <DropShadowEffect Color="Gray" BlurRadius="10" Direction="0" ShadowDepth="0" Opacity="0.5"  />
                        </CheckBox.Effect>
                    </CheckBox>
                </Grid>
            </Border>
        </DataTemplate>
复制代码

效果如下:

 以上作为记录

posted on   lopengye  阅读(879)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示