如何将数据命令绑定到驻留在数据网格中的按钮

时间:2012-09-05 07:04:12

标签: c# wpf button datagrid

  

可能重复:
  Bind command from Business object to View in 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;
            }

        } 

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

1 个答案:

答案 0 :(得分:0)

在绑定中使用RelatieSource指定按钮的命令绑定。

<Button Command ="{Binding DataContext.BanquetSearchCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}}" />