This is a migrated thread and some comments may be shown as answers.

Binding to a view in c#

4 Answers 81 Views
TabControl
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Andrew asked on 01 Oct 2010, 05:45 PM
I am using your menu control to let users navigate, and I am using your tab control to show the views that they select.  I do not want to close any tabs (views) automatically when they move between views, and I want to allow multiple instances of the same view, for example a product search, or a shopping cart.

so this is what I have roughed out: first the xaml
<telerik:RadMenu x:Name="MainMenu" ClickToOpen="False" ItemClick="MainMenu_ItemClick" VerticalAlignment="Top" HorizontalAlignment="Center"
           Grid.Row="0" Grid.Column="1" >
     <telerik:RadMenuItem Header="Manufacturers" >
         <telerik:RadMenuItem Header="List" Name="Manufacturers" >
         </telerik:RadMenuItem>
         <telerik:RadMenuItem Header="Detail" Name="Manufacturer" />
      </telerik:RadMenuItem>
     <telerik:RadMenuItem Header="Customers">
             <telerik:RadMenuItem Header="List" Name="Customers" />
             <telerik:RadMenuItem Header="Detail" Name="Customer" />
     </telerik:RadMenuItem>
     <telerik:RadMenuItem Header="Distributors" Name="Distributors">
           
     </telerik:RadMenuItem>
     
 </telerik:RadMenu>
And to setup the tabcontrol
<telerik:RadTabControl FontSize="15" x:Name="tabControl" Margin="10" Grid.Row="1" Grid.Column="1"
                             >
    <telerik:RadTabItem Header="Home" />
</telerik:RadTabControl>
The first tab will hold the main landing page information (marketing stuff...)
private void MainMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
 {
     RadMenuItem item = e.OriginalSource as RadMenuItem;
     if (item != null)
     {
         RadTabItem itemToAdd = new RadTabItem()
         {
             Header = item.Name.ToString()
               
             // bind to view????
     
         };
         tabControl.Items.Add(itemToAdd);
         System.Windows.MessageBox.Show("You clicked '" + item.Header.ToString() + "'"+
             item.Name.ToString());
               
     }
 }

So the question is how do I bind the tab to my view.  The view name is in every case item.Name.ToString()+"View" so we could even create the name to bind to dynamically.

I assume I will need to initialize the view first.

I am doing all this using MVVM Light.

4 Answers, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
answered on 02 Oct 2010, 09:38 PM
I have the hard part figured out this will bind a view to a tab
if (item != null)
      {
          RadTabItem itemToAdd = new RadTabItem()
          {
            Header = item.Name.ToString(),
            Content= new CustomersView() 
      
          };
            
          tabControl.Items.Add(itemToAdd);
But as you can see I am specifying the view explicitly which will require a giant case statement.  What I want to do is to bind the view that has the name item.Name.ToString()+"View" or even item.Name.ToString since I can name the views anyway I like.

Does anyone know how to do that?  I suspect there is a way to use binding.

By the way someone should add this syntax to the Telerik tabcontrol documentation. 
0
Zarko
Telerik team
answered on 06 Oct 2010, 10:01 AM
Hello Andrew,

Unfortunately in this scenario you cannot use binding, because RadMenu have no SelectedItem property or something similar, so you should handle ItemClick event and place your logic there (the switch statement will work fine).

Sincerely yours,
Zarko
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
answered on 09 Oct 2010, 04:39 AM
So now i have my view bound to a tab, and I can interact with controls on the bound view.  I added a button to test something, and although I click on it, it never executes its MouseLeftButtonDown method.  Is there any reason why this would happen.  In XAML

 

 

 

 

<Button Content="Close" Height="23" Name="CloseButton" Width="75"  MouseLeftButtonDown="CloseButton_MouseLeftButtonDown"   HorizontalAlignment="Center" />

In code

 

private void CloseButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    MessageBox.Show("close requested");
    Messenger.Default.Send("CustomerTabClosed");
}
The messagebox never apprears.


0
Andrew
Top achievements
Rank 1
answered on 09 Oct 2010, 06:24 PM
I am way too dense.  button events are Click not leftmousebuttondown - setup a new event handler, and presto, it works, and now my tabs magically close.
Tags
TabControl
Asked by
Andrew
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Zarko
Telerik team
Share this question
or