Search This Blog

Monday, September 16, 2013

Tab Control Style WPF


                
     

Source : http://www.wpf-tutorial.com/tabcontrol/styling-the-tabitems/


The WPF Tab Control

Let's start off easy by simply putting a regular old tab control in a window and adding a couple of tabs.
 x:Class="TabControlTutorial.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF Tabs" Height="281" Width="454">
  
    
       Header="Cheese">
        The Cheese Tab
      
       Header="Pepperoni">
        The Pepperoni Tab
      
       Header="Mushrooms">
        The Mushrooms Tab
      
    
The above code gives you a pretty standard looking tab control like the one pictured below.
Basic Tab
Control
Like most other WPF controls, the contents of a TabItem can be most any other WPF control. Let's look at a tab with an image set as the content.
 x:Class="TabControlTutorial.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF Tabs" Height="281" Width="454">
  
    
       Header="Cheese">
        The Cheese Tab
      
       Header="Pepperoni">
         Source="pepperoni.jpg" />
      
       Header="Mushrooms">
        The Mushrooms Tab
      
    
That should look something like the following:
Pepperoni Tab
Control
That's enough of the simple stuff. If all you want is a basic tab control with some content on each tab, the above code will do everything you need. Let's look at some more interesting stuff now. Just like the content of the TabItem, the Header property of the TabItem can also have other WPF controls. Let's put an image in each of the tabs.
 x:Class="TabControlTutorial.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF Tabs" Height="281" Width="454">
  
    
      
        
           Orientation="Horizontal">
             Height="18" Source="cheese.jpg" />
             Text="Cheese" Margin="2,0,0,0" VerticalAlignment="Center" />
          
        
Orientation="Horizontal"> Height="18" Source="pepperoni.jpg" /> Text="Pepperoni" Margin="2,0,0,0" VerticalAlignment="Center" /> Orientation="Horizontal"> Height="18" Source="mushrooms.jpg" /> Text="Mushrooms" Margin="2,0,0,0" VerticalAlignment="Center" />
And that makes something that looks like the image below.
Tab Control With
Images
With the techniques above, you should be able to do almost anything you need with a tab control. What you can't do, however, is change how the underlining tab control looks. Fortunately, WPF's style system makes modifying how the tab control looks fairly easy. By default, the color of the tabs themselves will follow the theme of the Windows machine they're running on. Let's start off by modifying the color and shape of the tabs.
The first thing we need to do is define a style for the TabItem control. Styles for WPF are similar to what CSS is for web pages, except much more powerful.
 x:Class="TabControlTutorial.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF Tabs" Height="281" Width="454">
  
    
  
  
      
         Header="Cheese" />
         Header="Pepperoni" />
         Header="Mushrooms" />
      
Styles are defined up in the resources for your control - in this case the Window that holds my tab control. There's a lot going on here, and most of it I stole from this MSDN article. Let's step through this tag-by-tag.
When a TabItem is clicked, its IsSelected property gets set to true. So what we have to do is add triggers watching the IsSelected property. When the property becomes true, the background color is set to light blue. When it's false, the background color is changed to light gray. Now we've got something that looks like this:
Tabs With
Triggers
I think we've got pretty good control over the tabs. Now I want to change how the background and borders look on the tab contents. The style for the tab control is very similar to what we just did for the tab items.
With that code we now have a tab control that looks like this:
Tab Control With Styled
Contents
I think that about does it for the WPF TabControl. In this tutorial you've learned how to create and populate a basic tab control as well as more advanced techniques for styling the tabs and tab contents. I think the learning curve for WPF styles is a little steep, but once you understand it, you can see how powerful it is. The best thing, however, is that I didn't have to write any C# code to skin my tab control.

Friday, September 13, 2013

List box sorting c#

listBox1.Items.SortDescriptions.Add(
    new System.ComponentModel.SortDescription("Content",
       System.ComponentModel.ListSortDirection.Ascending ));

Friday, July 12, 2013

Change Background of an MDI Form

foreach (Control control in this.Controls)
    {
        // #2
        MdiClient client = control as MdiClient;
        if (!(client == null))
        {
         // #3
         client.BackColor = GetYourColour();
         // 4#
         break;
        }
    }

put this code in page load.

Sunday, June 30, 2013

disable a combobox in WinForms without graying it out

Setting these on your comobobox will do the trick you are looking for, Combo is enabled but nobody can change or type anything so Appearance = Enabled, Behaviour = Disabled :)
        comboBox1.DropDownHeight = 1;
        comboBox1.KeyDown += (s, e) => e.Handled = true;
        comboBox1.KeyPress += (s, e) => e.Handled = true;
        comboBox1.KeyUp += (s, e) => e.Handled = true;
If for some reason you cannot use lambdas then following handlers can be associated. Right Click -> Paste has to be handled additionally if you have DropDownStyle = DropDown.
    //void comboBox1_KeyUp(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
    //{
    //    e.Handled = true;
    //}

    //void comboBox1_KeyDown(object sender, KeyEventArgs e)
    //{
    //    e.Handled = true;
    //}