Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
285
Rendering different grid rows
posted

I have a ObservableCollection<Item> collection which I am using as a grid items source. The Item class objects have a property whose value dictates how the grid row for that object should look like. In most cases, rows will be same, with textboxes and checkboxes. However, for a few certain values, the row should only display a dropdown/combobox in the first cell and gray out the rest of the cells. The dropdown should be populated somehow with a collection that is retrieved while the control is loaded and the selected value should be the value of the Item.ListName property.

Is there any way of achieving this, like using DataTemplate/ContentControl/IValueConverter implementations?

Thanks

Parents
No Data
Reply
  • 240
    posted

    Quick & dirty way to do that, but not necessarily the best:

    • define all the controls that you might want in a DataTemplate (say combobox, checkbox, textblock)
    • define boolean properties like IsCombo, IsCheckbox, IsTextBlock for your type (Item?)
    • create a Boolean to Visibility value converter (see below)
    • bind the Visibility property of each control (combobox, checkbox, textblock, etc.) to the property you defined for your bound Item class, using the converter you defined (<CheckBox Visibility="{Binding IsCheckBox, Converter={StaticResource BoolToVisibilityConverter}}" />)
    • if you want to really just disable the controls instead of hide them, you could just bind to the IsEnabled property instead
    • populating the dropdown/selecting value is explained in some other posts on here in the XamWebGrid section of the forum
    • 2-way databind the property that you want for the value

    Bool to visibility converter:

     

     

     

     

     

     

     

     

    Public

     

    Class BoolToVisibilityConverter

    Implements IValueConverter

     

     

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert

     

     

    Return IIf(CType(value, Boolean), Visibility.Visible, Visibility.Collapsed)

     

     

    End Function

     

     

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack

     

     

    Return (CType(value, Visibility) = Visibility.Visible)

     

     

    End Function

    End Class

     

     

     

     

     

     

     

     

     

    Public

     

     

    Class

    BoolToVisibilityConverter

     

     

    Implements

    IValueConverter

     

     

    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements

    System.Windows.Data.IValueConverter.Convert

     

     

    Return IIf(CType(value, Boolean

    ), Visibility.Visible, Visibility.Collapsed)

     

     

    End

    Function

     

     

     

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As CultureInfo) As Object Implements

    System.Windows.Data.IValueConverter.ConvertBack

     

     

    Return (CType

    (value, Visibility) = Visibility.Visible)

     

     

    End

    Function

    End

     

     

    Class

     

     

Children
No Data