none
IContentItem.IsReadOnly false where it should be true RRS feed

  • Question

  • I am creating a small business type. I have created 2 editor controls. One for datagrid and one for detail.

    I have managed to add IsReadOnly property to my grid control and on Control_Load (Datacontext as IContentItem).IsReadOnly is True when I use Read-Only Controls in the designer.

    The same does not happen for the detail though. Although I set Use Read-Only Controls in the detail Layout Group, (Datacontext as IContentItem).IsReadOnly is always false. The rest of the controls are renderend as read-only though. I believe I am doing something wrong in casting the Datacontext. Maybe in detail the Datacontext is not the item but something else? Databinding though works correct according to my assumption.

    Any help would be appreciated.

     


    1+1 = 3 for large values of 1
    Tuesday, October 4, 2011 6:43 AM

Answers

  • When providing a default control for a business type, you cannot specify one control for using within a detail control and a second control to be used within the data grid.

    However, you can have one control but have the template of the control reconfigure itself based on whether the control is used within a collection control or if it is used outside of a collection control (e.g. a details or group control).

    Your control can bind to the ContainerState property on your data context. This property returns an enum of type ContainerState where the values that you would be concerned with are None and Cell. Cell corresponds with the control being used in a collection control whereas None corresponds with the control being used elsewhere.

    Here's what you can do to enable reacting to this property from your control:

    • Add a dependency property of type ContainerState to your control.
    • Add a callback to the dependency property to react to changes in the property.
    • Bind this property to the ContainerState property on your data context.

    You can write the behavior changing from code or your can take a few more steps to enable the control template to react to the change in the property:

    • Add two visual state attributes to your control; they should correspond to the None and Cell values of the ContainerState enum.
    • From your dependency property callback, change the visual state of the control to one of these visual states using the VisualStateManager.
    • In your control template, add a VisualStateGroup with the two visual states. Use storyboards to animate the elements within the control template to react to the state change.

    Here's some MSDN docs on using the Parts and States model within Silverlight: http://msdn.microsoft.com/en-us/library/cc189093(v=VS.95).aspx#changing_the_appearance_of_a_control_depending_on_its_state


    Justin Anderson, LightSwitch Development Team
    Tuesday, October 4, 2011 9:13 AM
    Moderator

All replies

  • Setting "Use read-only controls" doesn't affect the IsReadOnly property of content items. Setting this property to true (Use read-only controls) tells to the runtime to visualize the content items with the viewer control instead of the editor control. For example, if a Rows Layout has Text Box controls and you set Use read-only controls property to true, the designer will flip those Text Box controls to be Label controls.
    Justin Anderson, LightSwitch Development Team
    Tuesday, October 4, 2011 6:57 AM
    Moderator
  • Thanks for the immediate reply Justin

    Reading back this article

    http://msdn.microsoft.com/en-us/library/hh290141/#BKMK_AddRead-OnlySupport

    I realize I did not get it all right. I was almost sure there must be 2 controls 1 for editing and 1 for viewing but I was missled (on my own liability) by this article. Could you please enlighten my on how to support this? I already know about DefaultViewMapping attribute that looks like it could be used but this is for the business type and I haven't found a way to implement more that one of these. Can you suggest related article or something? In the same area I believe is having one control for collections and another for details to be automatically selected.

    Thanks in advance


    1+1 = 3 for large values of 1
    Tuesday, October 4, 2011 7:59 AM
  • When providing a default control for a business type, you cannot specify one control for using within a detail control and a second control to be used within the data grid.

    However, you can have one control but have the template of the control reconfigure itself based on whether the control is used within a collection control or if it is used outside of a collection control (e.g. a details or group control).

    Your control can bind to the ContainerState property on your data context. This property returns an enum of type ContainerState where the values that you would be concerned with are None and Cell. Cell corresponds with the control being used in a collection control whereas None corresponds with the control being used elsewhere.

    Here's what you can do to enable reacting to this property from your control:

    • Add a dependency property of type ContainerState to your control.
    • Add a callback to the dependency property to react to changes in the property.
    • Bind this property to the ContainerState property on your data context.

    You can write the behavior changing from code or your can take a few more steps to enable the control template to react to the change in the property:

    • Add two visual state attributes to your control; they should correspond to the None and Cell values of the ContainerState enum.
    • From your dependency property callback, change the visual state of the control to one of these visual states using the VisualStateManager.
    • In your control template, add a VisualStateGroup with the two visual states. Use storyboards to animate the elements within the control template to react to the state change.

    Here's some MSDN docs on using the Parts and States model within Silverlight: http://msdn.microsoft.com/en-us/library/cc189093(v=VS.95).aspx#changing_the_appearance_of_a_control_depending_on_its_state


    Justin Anderson, LightSwitch Development Team
    Tuesday, October 4, 2011 9:13 AM
    Moderator
  • Thanks for the elaborate reply Justin. I realy appreciate it. I would also appreciate, risking to abuse your willingness to help, a hint about read-only capability. Is there something respective to the ContainerState informing me about read-only or not, so I could use the same approach, or should I do it another way?

    Thanks again

    Kostas Christodoulou


    1+1 = 3 for large values of 1
    Wednesday, October 5, 2011 6:13 AM
  • No problem; I'm always glad to help.

    It depends on what you want to use IsReadOnly for. If you just want to make, for example, a text box in your control read-only based on the IsReadOnly property on the IContentItem, I would just bind directly to it:

    <TextBox IsReadOnly="{Binding IsReadOnly}"/>
    

    Or if you want enable/disable a control based on the property, bind to it using a "not" converter (an IValueConverter that inverts a Boolean value):

    <Button IsEnabled="{Binding IsReadOnly, Converter={StaticResource NotConverter}}"/>
    
    If you want to do more complex things in your UI, such as changing brushes, margins, etc... I would implement it the same way as the ContainerState method above (create a dependency property, add a callback, etc...).


    Justin Anderson, LightSwitch Development Team
    Wednesday, October 5, 2011 8:19 AM
    Moderator
  • Hi Justin,

    I've been able to follow all of your steps, except "Your control can bind to the ContainerState property on your data context" & "Bind this property to the ContainerState property on your data context".

    Could you prehaps elaborate on that, or describe what that actually look like, where it goes etc.

    THanks for your help.


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Thursday, October 6, 2011 1:20 PM
  • *bump*

    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.

    Sunday, October 9, 2011 7:08 AM
  • Hi Justin,

    I've been able to follow all of your steps, except "Your control can bind to the ContainerState property on your data context" & "Bind this property to the ContainerState property on your data context".

    Could you prehaps elaborate on that, or describe what that actually look like, where it goes etc.

    THanks for your help.


    Yann

    (plus ça change, plus c'est la même chose!)

    If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer".

    This will help people find the answers that they're looking for more quickly.


    I think he means this:

     

    Also this may help:

    LightSwitch supports enabling controls to behave differently when they are displayed in a DataGrid or similar container control...

    http://msdn.microsoft.com/en-us/library/hh290136.aspx#BKMK_HndlKybdNav


    Make Them Ask: That's a LightSwitch App?

    http://LightSwitchHelpWebsite.com

    Sunday, October 9, 2011 11:57 AM