0

可能重复:
将来自业务对象的命令绑定到 MVVM 中的视图

                    <DataGridTemplateColumn Header="Edit" IsReadOnly="True" MinWidth="40" Width="*" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnedit" Width="25" Height="25" Command="{Binding BanquetSearchCommand, ElementName=LayoutRoot}" CommandParameter="{Binding Id}">
                                    <!--<Button.CommandParameter>
                                        <MultiBinding Converter="{StaticResource ResourceKey=BanquetValuConverter}">
                                            <Binding StringFormat="hi"></Binding>
                                        </MultiBinding>
                                    </Button.CommandParameter>-->
                                    <Image  Width="25" Height="15" Source="/TBC;component/images/Edit_N1.png" ></Image>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>

                    <DataGridTemplateColumn Header="Delete" IsReadOnly="True" MinWidth="40" Width="*" >
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="btnDelete" Width="25" Height="25">
                                    <Image  Width="25" Height="15" Source="/TBC;component/images/Delete_N1.png"></Image>
                                </Button>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>
    </Border>

    <Border CornerRadius="3" BorderBrush="Black" BorderThickness="1" Grid.Row="3" >
        <StackPanel Grid.Row="5" Orientation="Horizontal" Margin="300 0 0 0" Height="100">
            <Button Content="Add" Foreground="White" Height="23" Margin="38 38 0 38" HorizontalAlignment="Left" Name="btnAdd" VerticalAlignment="Top" Width="75" Command="{Binding BanquetSearchCommand}" >
                <Button.CommandParameter>
                    <MultiBinding Converter="{StaticResource ResourceKey=BanquetValuConverter}">
                        <Binding ElementName="btnAdd" Path="Content"/>
                        <Binding ElementName="btnRefresh" Path="Content"/>  

                    </MultiBinding>    
                </Button.CommandParameter>
                <Button.Background>
                    <ImageBrush ImageSource="/TBC;component/images/Add.png"  Stretch="UniformToFill" TileMode="None" />
                </Button.Background>
            </Button>
            <Button Content="    Refresh" Foreground="White" Height="23" Margin="20 38 0 38" HorizontalAlignment="Left"  Name="btnRefresh" VerticalAlignment="Top" Width="75">
                <Button.Background>
                    <ImageBrush ImageSource="/TBC;component/images/Refresh.png"  Stretch="UniformToFill" TileMode="None" />
                </Button.Background>
            </Button>
            <Button Content="Exit" Foreground="White" Height="23" Margin="20 38 0 38" HorizontalAlignment="Right"  Name="btnExit" VerticalAlignment="Top" Width="75">
                <Button.Background>
                    <ImageBrush ImageSource="/TBC;component/images/Exit.png" Stretch="UniformToFill" TileMode="None" />
                </Button.Background>
            </Button>
        </StackPanel>
    </Border>

以上是我想要的数据网格文本列数据单击编辑和删除按钮命令的xaml。下面是视图模型

public ICommand BanquetSearchCommand { get; set; }
 public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            //var param = (Tuple<string, string>)parameter;

            List<string> lststring = parameter as List<string>;
            string operation = string.Empty;
            string datarowindex = string.Empty;
            operation = Convert.ToString(lststring[0]).Trim();
            datarowindex = Convert.ToString(lststring[1]).Trim();

            switch (operation)
            {
                case "Add":
                    BlanquetMaster _bnkmaster = new BlanquetMaster();
                    _bnkmaster.ShowDialog();
                    break;
                case "Refresh":
                    break;
                case "Close":
                    break;
            }

        } 

请指导我编辑按钮不执行 Execute()。我是新的新WPF。

4

1 回答 1

0

在绑定中使用 RelatieSource 来指定按钮的 Command Binding。

<Button Command ="{Binding DataContext.BanquetSearchCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" />
于 2012-09-05T07:13:58.910 回答