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
- <DataGrid itemssource="{Binding videolist}">
- <datagrid.columns>
- <!--serial number --
- <datagridtextcolumn header="serial number" width="binding= "{Binding Index} "/ >
- <!--video name --
- <datagridtextcolumn header="video name" width=" binding=" {Binding name} " />
- <!--file size -
- <datagridtextcolumn header="File size" width="binding=" {Binding sizestring} " />
- <!--source Video address --
- <datagridtextcolumn header="source video address" width="280" binding="{Binding SourcePath}" />
- <!--status --
- <datagridtextcolumn header="status" width="binding=" {Binding statusstring} " />
- <!--upload Progress --
- <datagridtemplatecolumn header="Upload progress" width="260">
- <datagridtemplatecolumn.celltemplate>
- <DataTemplate>
- <ProgressBar value="{Binding Progress}" margin="20,0"/>
- </DataTemplate>
- </datagridtemplatecolumn.celltemplate>
- </datagridtemplatecolumn>
- <!--upload speed -
- <datagridtextcolumn header="upload speed" width= " binding=" {Binding speedstring} " />
- <!--operation
- <datagridtemplatecolumn header="Operation" minwidth=" " ">
- <datagridtemplatecolumn.celltemplate>
- <DataTemplate>
- <button content="{Binding operationstring}"
- command="{Binding Videooperationcommand}"
- commandparameter="{Binding relativesource={relativesource mode=findancestor, Ancestortype=datagrid}, Path= SelectedItem} " />
- </DataTemplate>
- </datagridtemplatecolumn.celltemplate>
- </datagridtemplatecolumn>
- </datagrid.columns>
- </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
- <!--operation
- <datagridtemplatecolumn header="Operation" minwidth=" " ">
- <datagridtemplatecolumn.celltemplate>
- <DataTemplate>
- <button content="{Binding operationstring}"
- command="{Binding Datacontext.videooperationcommand, Relativesource={relativesource mode=findancestor, Ancestortype=datagrid}} "
- commandparameter="{Binding relativesource={relativesource mode=findancestor, Ancestortype=datagrid}, Path= SelectedItem} " />
- </DataTemplate>
- </datagridtemplatecolumn.celltemplate>
- </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
- Commandparameter="{Binding Name}"
The button command bindings and command parameter bindings in the WPF DataGrid template