locked
Using XAML to fill a tabcontrol with 2 pages and 2 text boxes... Is it possible? RRS feed

  • Question

  • Hey people, I am using XAML about 3 weeks for now and currently I got a little problem on my mind... I hope you can help me :) I got the following XML code: <Region> <Reg><X>1</X><Y>3</Y></Reg> <Reg><X>4</X><Y>2</Y></Reg> </Region> And I want to fill this data into a tab control with 2 tab pages. On each tab page are 2 text boxes. One text box for X and one for Y. If I select the first page, in the text boxes should be the numbers 1 and 3 and if I select the second page, in the text boxes should be the numbers 4 and 2. Is this possible with just XAML? By using DataContext="{Binding Source={StaticResource Region}}" and ItemsSource="{Binding XPath=//Reg} I got my two tab pages, but how could I fill the text boxes depending on the selected page? Greetings,
    Monday, May 31, 2010 2:19 PM

Answers

  • You need to specify a content template on the tab control:

      <TabControl ItemsSource="{Binding XPath=Region/Reg}">
       <TabControl.ContentTemplate>
        <DataTemplate>
         <StackPanel>
          <TextBox Text="{Binding XPath=X}" />
          <TextBox Text="{Binding XPath=Y}" />
         </StackPanel>
        </DataTemplate>
       </TabControl.ContentTemplate>
      </TabControl>
    Note that this will generate 2 textboxes for each tab page. It's not possible to have only 2 textboxes and fill them depending on the selected page. To be precise that's possible too but not if you want the textboxes to be inside the tab pages.
    • Marked as answer by slaindevil Tuesday, June 1, 2010 7:56 AM
    Monday, May 31, 2010 2:37 PM

All replies

  • You need to specify a content template on the tab control:

      <TabControl ItemsSource="{Binding XPath=Region/Reg}">
       <TabControl.ContentTemplate>
        <DataTemplate>
         <StackPanel>
          <TextBox Text="{Binding XPath=X}" />
          <TextBox Text="{Binding XPath=Y}" />
         </StackPanel>
        </DataTemplate>
       </TabControl.ContentTemplate>
      </TabControl>
    Note that this will generate 2 textboxes for each tab page. It's not possible to have only 2 textboxes and fill them depending on the selected page. To be precise that's possible too but not if you want the textboxes to be inside the tab pages.
    • Marked as answer by slaindevil Tuesday, June 1, 2010 7:56 AM
    Monday, May 31, 2010 2:37 PM
  • Thanks! Works! :D

     

    One little question. When I click on one of the tabs, the first textbox gets the focus automatically. Can I prevent this? I want no focus set to any of the textboxes, except if the user clicks on it or presses tab.

    Tuesday, June 1, 2010 7:56 AM
  • Is there a way, to automatically fill the headers of the TabItems with an incrementing value?

    So Tab 1 is called "1" and so on... By now I did this by using a Converter, but is there a better way?

     

    And is there a way, to automatically select the first child, after the TabControl is filled by the DataTemplate?

     

     

    Wednesday, June 2, 2010 12:14 PM