Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Border as control

Border as control

Scheduled Pinned Locked Moved WPF
helptutorial
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hema Bairavan
    wrote on last edited by
    #1

    Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please

    J A 2 Replies Last reply
    0
    • H Hema Bairavan

      Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please

      J Offline
      J Offline
      Jurgen Rohr
      wrote on last edited by
      #2

      Hi, you might want to consult the help about the VisualTreeHelper class. I think this provides the functionality you're looking for. If you need further assinstance, please provide some more concrete code/XAML. Cheers Jürgen

      H 1 Reply Last reply
      0
      • H Hema Bairavan

        Hi all , i want to clear my form in one loop. i did is that i put one outer canvas "OuterCanvas" example: clearing text box TextBox tx = new TextBox(); foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype()) ((TextBox)ctr).text = ""; here my problem is that if i have a border in the canvas i am unable to cast that one to the control. i am getting error; any idea any help please

        A Offline
        A Offline
        Abhinav S
        wrote on last edited by
        #3

        Hema Bairavan wrote:

        if(ctr.Gettype() == tx.Gettype())

        Why dont you just put in a condition to check for and avoid a border. foreeach(control ctr in OuterCanvas.controls) if(ctr.Gettype() == tx.Gettype() && ctr.Gettype() != System.Windows.Border) ((TextBox)ctr).text = "";

        My signature "sucks" today

        1 Reply Last reply
        0
        • J Jurgen Rohr

          Hi, you might want to consult the help about the VisualTreeHelper class. I think this provides the functionality you're looking for. If you need further assinstance, please provide some more concrete code/XAML. Cheers Jürgen

          H Offline
          H Offline
          Hema Bairavan
          wrote on last edited by
          #4

          _strTemp = _strEmpty; TextBox txtbox = new TextBox(); PasswordBox pwbox = new PasswordBox(); CheckBox ChkBox = new CheckBox(); foreach (Control Ctrl in cvInput.Children) { if (Ctrl.GetType() == txtbox.GetType()) ((TextBox)(Ctrl)).Text = _strTemp; if (Ctrl.GetType() == pwbox.GetType()) ((PasswordBox)(Ctrl)).Password = _strTemp; if (Ctrl.GetType() == ChkBox.GetType()) ((CheckBox)(Ctrl)).IsChecked = false; } this is my code.. i am getting error in the sentence.. foreach (Control Ctrl in cvInput.Children) the exception is.. Unable to cast object of type 'System.Windows.Controls.Border' to type 'System.Windows.Controls.Control'. I am getting the same error for the Image also.. what may be the reason>>??

          J 1 Reply Last reply
          0
          • H Hema Bairavan

            _strTemp = _strEmpty; TextBox txtbox = new TextBox(); PasswordBox pwbox = new PasswordBox(); CheckBox ChkBox = new CheckBox(); foreach (Control Ctrl in cvInput.Children) { if (Ctrl.GetType() == txtbox.GetType()) ((TextBox)(Ctrl)).Text = _strTemp; if (Ctrl.GetType() == pwbox.GetType()) ((PasswordBox)(Ctrl)).Password = _strTemp; if (Ctrl.GetType() == ChkBox.GetType()) ((CheckBox)(Ctrl)).IsChecked = false; } this is my code.. i am getting error in the sentence.. foreach (Control Ctrl in cvInput.Children) the exception is.. Unable to cast object of type 'System.Windows.Controls.Border' to type 'System.Windows.Controls.Control'. I am getting the same error for the Image also.. what may be the reason>>??

            J Offline
            J Offline
            Jurgen Rohr
            wrote on last edited by
            #5

            Hi, your problem has nothing to do with WPF; it's about C# fundamentals. You might want to take a look at the inheritance tree. There you can see why you get that error. Neither Border nor Image derive from Control. To be honest (really no flaming or insulting intended): The best advice I can give you, is to take a good book about C#- or .Net basics and a little time. In case I'm mistaken, I'll give you some clues (which hopefully will only help you if I'm mistaken): 1. ugly solution: use the VisualTreeHelper class. 2. ugly solution: use a common ancestor 3. good solution: use data binding Both ugly solutions (as well as the provided code) imply that you will never use your own controls - or you'll get an endless list of "if ... GettYpe()". Solution #2 (as well as the provided code) still has to deal with the stacking of WPF objects (you will find the Border inside the Canvas, but you will not find the TextBox inside the Border). Unfortunately I can't recommend any C# book, but if it's up to WPF (and accordingly data binding), I've learned a lot from "WPF unleashed". Cheers Jürgen

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups