how to set properties for a custom control (p.e. from system.windows.controls)

Răspuns how to set properties for a custom control (p.e. from system.windows.controls)

  • 12 martie 2012 14:37
     
     

    Hello everybody,

    I have added a progressbar as custom control from System.Windows.Control to my LightSwitch screen.
    Now I want to set properties as "Minimum", "Maximum" and "Value", but do not know how to solve this.
    I studied the forum but cannot see any proper solution.
    Can anybody help me?

    Thanks in advance.

    Albert


    Albert Korstanje


    • Editat de AlbertKor 12 martie 2012 14:40
    •  

Toate mesajele

  • 13 martie 2012 06:52
    Moderator
     
     

    Use the FindControl extension method to either set bindings for the ProgressBar or to subscribe a ControlAvailable event handler to get a reference to the ProgressBar.

    Here's an example of how to set bindings:http://msdn.microsoft.com/en-us/library/gg406736.aspx

    If you search for "FindControl" in the forums, you'll find many threads on how to use the ControlAvailable event.


    Justin Anderson, LightSwitch Development Team

  • 13 martie 2012 12:04
     
     Răspuns propus

    Justin, Thanks.

    I checked the information and came up with the following solution:

    1) On the screen I defined a custom control "PrgBar" as System.Windows.Controls.Progressbar

    2) Private WithEvents Pb As System.Windows.Controls.ProgressBar

    3) in the "_InitializeDataWorkspace" routine:
              AddHandler Me.FindControl("PrgBar").ControlAvailable, AddressOf PrgBarAvailable

    4)  Private Sub PrgBarAvailable(Sender As Object, e As ControlAvailableEventArgs)
              Pb = CType(e.Control, System.Windows.Controls.ProgressBar)
         End Sub

    5) In some other routine:
             Pb.Minimum = 1      <----- gives error: "Invalid cross-thread access"
             Pb.Maximum = 100
             Pb.Value = 1

    What is wrong with the definition of "Pb"?

    Hope you can help


    Albert Korstanje

  • 23 martie 2012 11:07
     
      Are cod

    Albert try the following code (sorry for c#)

    this.Application.Details.Dispatcher.BeginInvoke(() =>

    {
    Pb.Minimum = 1;                
    Pb.Maximum = 100;
    Pb.Value;
    };


                    

    rdk

  • 23 martie 2012 18:47
    Moderator
     
      Are cod

    Ralf almost has it right, but not quite. Since you are trying to modify properties of Silverlight controls, you have to be on the main thread (instead of the logic thread of the screen):

    Dispatchers.Main.Invoke(Sub()
        Pb.Minimum = 1
        Pb.Maximum = 100
        Pb.Value = 1
    End Sub)


    Justin Anderson, LightSwitch Development Team

  • 26 martie 2012 21:56
     
     Răspuns Are cod

    Hello Justin,

    Thanks for your reply.
    Your solution is working OK, except that for "Dispatchers.Main.Invoke" I had to set it to: "Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke":

                Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(
                Sub()
                    Pb.Minimum = 1
                    Pb.Maximum = 100
                    Pb.Value = 1
                End Sub)

    Thanks again for spending time in solving my problem.


    Albert Korstanje

    • Marcat ca răspuns de AlbertKor 26 martie 2012 21:56
    •  
  • 26 martie 2012 22:02
     
     

    Ralf,

    Thanks for your reply that helped me solving the problem.
    It toke some time because I was upgrading to Visual Studio 2010 Professional, which was not successful immediately. 

    See the code in VB in my answer to Justin.


    Albert Korstanje