The button command bindings and command parameter bindings in the WPF DataGrid template

Source: Internet
Author: User
Tags wpf datagrid

Scenario: Video upload function, upload list using the DataGrid control, the video has different states for different operations, the last column in the DataGrid is the Action column, which is the button control. You want to click on the button to do the corresponding action, but set the button's command= "{Binding Videooperationcommand}" after the action is not triggered.

The XAML code is as follows:

[HTML]View Plaincopy
  1. <DataGrid itemssource="{Binding videolist}">
  2. <datagrid.columns>
  3. <!--serial number --
  4. <datagridtextcolumn header="serial number" width="binding= "{Binding Index} "/ >
  5. <!--video name --
  6. <datagridtextcolumn header="video name" width=" binding=" {Binding name} " />
  7. <!--file size -
  8. <datagridtextcolumn header="File size" width="binding=" {Binding sizestring} " />
  9. <!--source Video address --
  10. <datagridtextcolumn header="source video address" width="280" binding="{Binding SourcePath}" />
  11. <!--status --
  12. <datagridtextcolumn header="status" width="binding=" {Binding statusstring} " />
  13. <!--upload Progress --
  14. <datagridtemplatecolumn header="Upload progress" width="260">
  15. <datagridtemplatecolumn.celltemplate>
  16. <DataTemplate>
  17. <ProgressBar value="{Binding Progress}" margin="20,0"/>
  18. </DataTemplate>
  19. </datagridtemplatecolumn.celltemplate>
  20. </datagridtemplatecolumn>
  21. <!--upload speed -
  22. <datagridtextcolumn header="upload speed" width= " binding=" {Binding speedstring} " />
  23. <!--operation
  24. <datagridtemplatecolumn header="Operation" minwidth=" " ">
  25. <datagridtemplatecolumn.celltemplate>
  26. <DataTemplate>
  27. <button content="{Binding operationstring}"
  28. command="{Binding Videooperationcommand}"
  29. commandparameter="{Binding relativesource={relativesource mode=findancestor, Ancestortype=datagrid}, Path= SelectedItem} " />
  30. </DataTemplate>
  31. </datagridtemplatecolumn.celltemplate>
  32. </datagridtemplatecolumn>
  33. </datagrid.columns>
  34. </DataGrid>

Analysis: Because the ItemsSource of the DataGrid is bound to a videolist,videolist is a list of video classes, The DataContext of the control inside the DataGrid becomes the video, which is the binding of the control is the property of the videos, such as the name (binding= "{Binding name}"). There is no videooperationcommand in video, so the operation cannot be triggered.

Solve: Know the reason to say, the command of the button is bound to ViewModel inside the Videooperationcommand is good, and the DataContext of the DataGrid is ViewModel, then do it well:

[HTML]View Plaincopy
  1. <!--operation
  2. <datagridtemplatecolumn header="Operation" minwidth=" " ">
  3. <datagridtemplatecolumn.celltemplate>
  4. <DataTemplate>
  5. <button content="{Binding operationstring}"
  6. command="{Binding Datacontext.videooperationcommand, Relativesource={relativesource mode=findancestor, Ancestortype=datagrid}} "
  7. commandparameter="{Binding relativesource={relativesource mode=findancestor, Ancestortype=datagrid}, Path= SelectedItem} " />
  8. </DataTemplate>
  9. </datagridtemplatecolumn.celltemplate>
  10. </datagridtemplatecolumn>

From the above analysis, we know that you can directly for the command to pass a property of the current video, such as the video name:

[HTML]View Plaincopy
    1. Commandparameter="{Binding Name}"

The button command bindings and command parameter bindings in the WPF DataGrid template

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.