views:

122

answers:

1

I have a Silverlight Datagrid, I'd like to make certain cells readonly programmatically. Specifically I have a CellEditingTemplate, I'd like to turn the cell editing off or on depending on the value of CategoryTypeName (see the xmal below).

               <local:DataGridTemplateColumn Header="Category" >
                    <local:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding CategoryTypeName}"/>
                        </DataTemplate>
                    </local:DataGridTemplateColumn.CellTemplate>
                    <local:DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                                <ComboBox 
                                  Width="90"
                                  x:Name="CategoryCombo"
                                  ItemsSource="{Binding CategoryTypes}"
                                  DisplayMemberPath="Name"
                                  SelectionChanged="CategoryCombo_SelectionChanged"
                                  />
                        </DataTemplate>
                    </local:DataGridTemplateColumn.CellEditingTemplate>
                </local:DataGridTemplateColumn>

Is there a way to do this?

Any help would be very much appreciated.

Thanks in advance.

+1  A: 

One way to do it would be to have two controls overlapping each other in your CellEditingTemplate and only show the one you need. Something like this

<local:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <Grid>
            <ComboBox Visibility="{Binding CategoryTypeName, Converter={StaticResource ConverterToDecideIfItShouldBeEditable}}"
                      Width="90" x:Name="CategoryCombo"
                      ItemsSource="{Binding CategoryTypes}"
                      DisplayMemberPath="Name"
                      SelectionChanged="CategoryCombo_SelectionChanged" />
            <TextBox Text="{Binding CategoryTypeName}"  
                     Visibility="{Binding CategoryTypeName, Converter={StaticResource ConverterToDecideIfItShouldBeEditable},ConverterParameter=Inverse}"/>
        </Grid>
    </DataTemplate>
</local:DataGridTemplateColumn.CellEditingTemplate>

The key to this is the converter. The second textbox gives a ConverterParameter as 'Inverse' which the converter than uses to return the opposite value. Using this you can make the converter return Visibility.Visible for one of the controls and Visibility.Collapsed for the other control.

Stephan