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. WPF TabControl Events

WPF TabControl Events

Scheduled Pinned Locked Moved WPF
wpfcsharpdatabasedesignarchitecture
27 Posts 4 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.
  • L Lost User

    Tom Delany wrote:

    That sounds very sensible to me. Obviously (?) my ViewModels are not exactly the same. I guess I should find what is common among them and turn that into a base class

    While you should always be trying to find commonalities etc (re-factoring) don't worry about it to much. Remember you can always do this...

    public interface ITabItem
    {
    //Don't need anything, but here is where you can put your new event
    }

    Then just have all your VMs with in the collection use it. Then your collection is just ObservableCollection<ITabItem>

    Tom Delany wrote:

    Since the ItemSource is bound to the ObservableCollection, this would take care of setting the DataContext for each TabItem?

    Not exactly. When doing VM first, you really don't bind the data context object to the view's data context property. Instead you use DataTemplates. There are a couple (er many) ways to acheive this. The simplest to understand IMO is to define the DataTemplate to target that type of object, and pop in the view.

    *No VS right now, but I am pretty sure this is right. Place this DataTemplate in a resource dictionary and reference the dictionary in the UserControl that has the TabControl. It will then render the "OneOfYourViewModel" object as a "TheCorrespondingView" user control. Another way is to define the DataTemplate with a key and then reference it for your "ItemTemplate". This is why it is VM first. The VM is actually built first, and then when the UI wants to render it checks its datatemplate. If one is not set it will do what it knows how to, ToString and you will end up with the object name. The way you have explained is another methodology where the UserControls are constructed then they each (or some injection using a pattern) construct their datacontext.

    Tom Delany wrote:

    How would I turn this into a DataTemplate? What about the buttons?

    I hope it is clear about the user control. Just pop it into a DataTemplate and you are good to go. As for Buttons, they have a property 'Command' that is a DP which you can bind an ICommand object to (i.e. your RelayCommand in your VMs). For the most part you can avoid using 'events' they w

    T Offline
    T Offline
    Tom Delany
    wrote on last edited by
    #18

    Thanks Collin. Clear as mud. ;) Actually, I think I understand what you mean. A couple of things I feel a little fuzzy about, but I think I have enough info. to sort it out. If I run up against something I can't figure out, hopefully you won't mind if I bug you again. :-O

    WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

    L 1 Reply Last reply
    0
    • L Lost User

      Tom Delany wrote:

      That sounds very sensible to me. Obviously (?) my ViewModels are not exactly the same. I guess I should find what is common among them and turn that into a base class

      While you should always be trying to find commonalities etc (re-factoring) don't worry about it to much. Remember you can always do this...

      public interface ITabItem
      {
      //Don't need anything, but here is where you can put your new event
      }

      Then just have all your VMs with in the collection use it. Then your collection is just ObservableCollection<ITabItem>

      Tom Delany wrote:

      Since the ItemSource is bound to the ObservableCollection, this would take care of setting the DataContext for each TabItem?

      Not exactly. When doing VM first, you really don't bind the data context object to the view's data context property. Instead you use DataTemplates. There are a couple (er many) ways to acheive this. The simplest to understand IMO is to define the DataTemplate to target that type of object, and pop in the view.

      *No VS right now, but I am pretty sure this is right. Place this DataTemplate in a resource dictionary and reference the dictionary in the UserControl that has the TabControl. It will then render the "OneOfYourViewModel" object as a "TheCorrespondingView" user control. Another way is to define the DataTemplate with a key and then reference it for your "ItemTemplate". This is why it is VM first. The VM is actually built first, and then when the UI wants to render it checks its datatemplate. If one is not set it will do what it knows how to, ToString and you will end up with the object name. The way you have explained is another methodology where the UserControls are constructed then they each (or some injection using a pattern) construct their datacontext.

      Tom Delany wrote:

      How would I turn this into a DataTemplate? What about the buttons?

      I hope it is clear about the user control. Just pop it into a DataTemplate and you are good to go. As for Buttons, they have a property 'Command' that is a DP which you can bind an ICommand object to (i.e. your RelayCommand in your VMs). For the most part you can avoid using 'events' they w

      T Offline
      T Offline
      Tom Delany
      wrote on last edited by
      #19

      Collin Jasnoch wrote:

      Place this DataTemplate in a resource dictionary and reference the dictionary in the UserControl that has the TabControl. It will then render the "OneOfYourViewModel" object as a "TheCorrespondingView" user control.

      Create a ResourceDictionary under Application.Resources or in the MainWindows under Window.Resources, or does it really matter as long as it's closer to the root than where it is used?

      Collin Jasnoch wrote:

      reference the dictionary in the UserControl that has the TabControl.

      Will you show me a XAML example of what you mean?

      Collin Jasnoch wrote:

      Another way is to define the DataTemplate with a key and then reference it for your "ItemTemplate".

      I thought I had figured this way out, but then I stumbled because I have multiple DataTemplates and I was not sure if I still created a ResourceDictionary and gave it a key, and then bound that to the "ItemTemplate" of the TabControl, or if I had to assign a key to each template and somehow bind each one to ... something :confused:? Sorry. I'm a moron... :sigh:

      WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

      P 1 Reply Last reply
      0
      • T Tom Delany

        Collin Jasnoch wrote:

        Place this DataTemplate in a resource dictionary and reference the dictionary in the UserControl that has the TabControl. It will then render the "OneOfYourViewModel" object as a "TheCorrespondingView" user control.

        Create a ResourceDictionary under Application.Resources or in the MainWindows under Window.Resources, or does it really matter as long as it's closer to the root than where it is used?

        Collin Jasnoch wrote:

        reference the dictionary in the UserControl that has the TabControl.

        Will you show me a XAML example of what you mean?

        Collin Jasnoch wrote:

        Another way is to define the DataTemplate with a key and then reference it for your "ItemTemplate".

        I thought I had figured this way out, but then I stumbled because I have multiple DataTemplates and I was not sure if I still created a ResourceDictionary and gave it a key, and then bound that to the "ItemTemplate" of the TabControl, or if I had to assign a key to each template and somehow bind each one to ... something :confused:? Sorry. I'm a moron... :sigh:

        WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #20

        Tom Delany wrote:

        Create a ResourceDictionary under Application.Resources

        Typically, you create an external ResourceDictionary file and then reference it in your App.Xaml (in the ResourceDictionary section). This would look something like this:

        <Application.Resources>
        <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="AppResources.xaml" />
        </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
        </Application.Resources>

        Forgive your enemies - it messes with their heads

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        T 1 Reply Last reply
        0
        • P Pete OHanlon

          Tom Delany wrote:

          Create a ResourceDictionary under Application.Resources

          Typically, you create an external ResourceDictionary file and then reference it in your App.Xaml (in the ResourceDictionary section). This would look something like this:

          <Application.Resources>
          <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="AppResources.xaml" />
          </ResourceDictionary.MergedDictionaries>
          </ResourceDictionary>
          </Application.Resources>

          Forgive your enemies - it messes with their heads

          "Mind bleach! Send me mind bleach!" - Nagy Vilmos

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          T Offline
          T Offline
          Tom Delany
          wrote on last edited by
          #21

          Thanks Pete. I'm still struggling with how to make the TabControl use the templates the way he suggested above. I created the ITabItem interface, and the ObservableCollection of ITabItems as he suggested. I bound the TabControl ItemSource to the ITabItem ObservableCollection (which is the collection of ViewModels). I figured out how to create a DataTemplate for each ViewModel as per Collin's suggestion. I think I could figure out how to bind one specific DataTemplate to the TabControl ItemTemplate, but I have multiple DataTemplates... Obviously I can't bind the Dictionary to the ItemTemplate. :sigh: :confused: Problem is, I can't even seem to come up with the right combination of words to Google for...

          WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

          P 1 Reply Last reply
          0
          • T Tom Delany

            Thanks Pete. I'm still struggling with how to make the TabControl use the templates the way he suggested above. I created the ITabItem interface, and the ObservableCollection of ITabItems as he suggested. I bound the TabControl ItemSource to the ITabItem ObservableCollection (which is the collection of ViewModels). I figured out how to create a DataTemplate for each ViewModel as per Collin's suggestion. I think I could figure out how to bind one specific DataTemplate to the TabControl ItemTemplate, but I have multiple DataTemplates... Obviously I can't bind the Dictionary to the ItemTemplate. :sigh: :confused: Problem is, I can't even seem to come up with the right combination of words to Google for...

            WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #22

            If you take a look at my article here[^], you should be able to see how I hooked up the DataTemplate elements to the TreeView for different ViewModels. Hopefully that should help.

            Forgive your enemies - it messes with their heads

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            T 1 Reply Last reply
            0
            • P Pete OHanlon

              If you take a look at my article here[^], you should be able to see how I hooked up the DataTemplate elements to the TreeView for different ViewModels. Hopefully that should help.

              Forgive your enemies - it messes with their heads

              "Mind bleach! Send me mind bleach!" - Nagy Vilmos

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              T Offline
              T Offline
              Tom Delany
              wrote on last edited by
              #23

              Thanks Pete. I'll take a look at it. Sorry about the stupid questions. After rereading the MSDN documentation about DataTemplates, and your suggestion about the ResourceDictionary (and removing the x:Key elements from my data templates :doh:) I got it to actually sort of work. The DataTemplate documentation finally keyed me in on the fact that the way Collin was suggesting I do the templates using "DataType" would cause the DataTemplate to be "magically" applied for that type. That was what I was not seeing in my mind... Now if I can just figure out how to make the tabs look like tabs again with headers at the top... :sigh: Always something with WPF...

              WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

              P 1 Reply Last reply
              0
              • T Tom Delany

                Thanks Pete. I'll take a look at it. Sorry about the stupid questions. After rereading the MSDN documentation about DataTemplates, and your suggestion about the ResourceDictionary (and removing the x:Key elements from my data templates :doh:) I got it to actually sort of work. The DataTemplate documentation finally keyed me in on the fact that the way Collin was suggesting I do the templates using "DataType" would cause the DataTemplate to be "magically" applied for that type. That was what I was not seeing in my mind... Now if I can just figure out how to make the tabs look like tabs again with headers at the top... :sigh: Always something with WPF...

                WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #24

                Tom - they aren't stupid questions, they are just questions about doing something differently. You have to remember that declarative programming is a lot different to the world of traditional coding; getting your head around it isn't easy, and a lot of very smart people have spent a lot of time figuring how to get this all to hang together. I've read articles by these smart people. :-D

                Forgive your enemies - it messes with their heads

                "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                T 1 Reply Last reply
                0
                • P Pete OHanlon

                  Tom - they aren't stupid questions, they are just questions about doing something differently. You have to remember that declarative programming is a lot different to the world of traditional coding; getting your head around it isn't easy, and a lot of very smart people have spent a lot of time figuring how to get this all to hang together. I've read articles by these smart people. :-D

                  Forgive your enemies - it messes with their heads

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  T Offline
                  T Offline
                  Tom Delany
                  wrote on last edited by
                  #25

                  I've been reading articles lately until my head hurts. They just don't seem to soak in. I feel like you and those other smart people are in a class way above me...

                  WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                  1 Reply Last reply
                  0
                  • T Tom Delany

                    Thanks Collin. Clear as mud. ;) Actually, I think I understand what you mean. A couple of things I feel a little fuzzy about, but I think I have enough info. to sort it out. If I run up against something I can't figure out, hopefully you won't mind if I bug you again. :-O

                    WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #26

                    Hope you got everything straightend out. I was out of the office yesturday so not hitting up CP much. Nothing 'silly' or 'dumb' about your questions. The only true 'dumb' question here on CP is when someone makes no effeort and asks "plz will u gimme codez" Otherwise you will find a lot of support. In fact the CP community is what got me up and running and for that I come back often and return the favor (and still get to be set straight :) ) WPF/Silverlight does have a strong learning curve so don't feel like you are alone. Most of the people answering the questions also struggled with it, but are just a couple years ahead of the game. Take bennefit from it and ask so you do not make the same mistakes the early birds did :-D

                    Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                    T 1 Reply Last reply
                    0
                    • L Lost User

                      Hope you got everything straightend out. I was out of the office yesturday so not hitting up CP much. Nothing 'silly' or 'dumb' about your questions. The only true 'dumb' question here on CP is when someone makes no effeort and asks "plz will u gimme codez" Otherwise you will find a lot of support. In fact the CP community is what got me up and running and for that I come back often and return the favor (and still get to be set straight :) ) WPF/Silverlight does have a strong learning curve so don't feel like you are alone. Most of the people answering the questions also struggled with it, but are just a couple years ahead of the game. Take bennefit from it and ask so you do not make the same mistakes the early birds did :-D

                      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

                      T Offline
                      T Offline
                      Tom Delany
                      wrote on last edited by
                      #27

                      Not really... :^) http://www.codeproject.com/Messages/4126589/UserControl-as-a-TabItem.aspx[^]

                      WE ARE DYSLEXIC OF BORG. Refutance is systile. Your a$$ will be laminated. There are 10 kinds of people in the world: People who know binary and people who don't.

                      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