开发者

How to trigger PreparingCellForEdit event from a (Edit)button click in Silver light 4

I have a Data Grid in Silverlight 4 with 3 columns along with a column which contains "Edit/Apply" button.

The row cells are in开发者_开发技巧itially rendered as plain text and I need them to be changed to Comboboxes in the edit mode.

Once the Edit button in any of the row is clicked. I need to change the textblock( This is my Cell Template) in one of the row to the ComboBox(This is my Cell Editing template)

The question is how do i facilitate this on clicking the Edit button of each row and not by double clicking on the row.

Thanks, Vijay


  • 1st way

    Put the textblocks on top of the combo-boxes (comboboxes with collapsed visibility). On Edit Switch visibilities between controls (Combo - visible / TextBlock - Collapsed) and Bind the Text Property from the Textblock to the selected value from the combo.

  • 2nd way

    Put only combo-boxes with IsReadOnly Property set to True. On Edit set IsReadOnly to false and on save set it back to true.*

  • 3rd way

    Make the datagrid readonly and bind a Data Form to it. The Data Form contains edit / save / cancel buttons.

    If you need an example just let me know and I'll write one as soon as possible.

    Not sure if this is what you expected. If not, please just ignore it. It is possible that I missunderstood the question.

  • Another answer

    The other answer will be to use a DelegateCommand binded on the Command property of the Edit button wich can contain a parameter (the row number). This is if you are using the MVVM pattern. And in the ViewModel you could edit the selected row.



After a bit of searching / trying i was able to toggle between display and edit mode by a button click (button placed in each row).

Below posted is the sample code , which facilitates this toggle for one of the Cells in the Grid, Which makes use of Two Boolean Properties ShowDefaultTemplate and ShowEditableTemplate , The VisibilityConverter converts the boolean values to corresponding Visibility Options (Visible or Collapsed).

<DataTemplate>
           <StackPanel Orientation="Horizontal">
                     <TextBlock Text="{Binding XXX}" HorizontalAlignment="Center" VerticalAlignment="Center"
                                               Visibility="{Binding ShowDefaultTemplate, Converter={StaticResource visibilityConverter}}" />
                     <ComboBox HorizontalAlignment="Left"  MinHeight="24" Width="100" 


                                              ItemsSource="{Binding Source, Source={StaticResource Provider}}" 
                                              Visibility="{Binding ShowEditableTemplate , Converter={StaticResource visibilityConverter}}" 
                                              SelectedItem = "{Binding SelctedItem,Mode=TwoWay}" />

            </StackPanel>
 </DataTemplate>

Thanks, Vijay

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜