WPF TabItem Custom ContentTemplate

Posted by lloydsparkes on Stack Overflow See other posts from Stack Overflow or by lloydsparkes
Published on 2010-03-19T11:07:15Z Indexed on 2010/03/19 11:11 UTC
Read the original article Hit count: 1145

Filed under:
|
|
|

I have been strugging with this for a while, it would have been simple to do in WindowForms.

I am making a IRC Client, there will be a number of Tabs one for each channel connect to. Each Tab needs to show a number of things, UserList, MessageHistory, Topic.

In WindowForms i would just have inherited from TabItem, added some Custom Properties, and Controls, and done.

In WPF i am having some slight issues with working out how to do it.

I have tried many ways of doing it, and below is my current method, but i cannot get the TextBox to bind to the Topic Property.

    <Style TargetType="{x:Type t:IRCTabItem}" BasedOn="{StaticResource {x:Type TabItem}}" >
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="540" />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <StackPanel Grid.Column="0">
                        <TextBox Text="{Binding Topic, RelativeSource={RelativeSource AncestorType={x:Type t:IRCTabItem}}}" />   
                    </StackPanel>
                </Grid>
            </DataTemplate>              
        </Setter.Value>
    </Setter>
</Style>

And the Codebehind

    public class IRCTabItem : TabItem
{
    static IRCTabItem()
    {
        //This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
        //This style is defined in themes\generic.xaml
        //DefaultStyleKeyProperty.OverrideMetadata(typeof(IRCTabItem),
         //   new FrameworkPropertyMetadata(typeof(IRCTabItem)));
    }

    public static readonly RoutedEvent CloseTabEvent =
        EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble,
            typeof(RoutedEventHandler), typeof(IRCTabItem));

    public event RoutedEventHandler CloseTab
    {
        add { AddHandler(CloseTabEvent, value); }
        remove { RemoveHandler(CloseTabEvent, value); }
    }

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        Button closeButton = base.GetTemplateChild("PART_Close") as Button;
        if (closeButton != null)
            closeButton.Click += new System.Windows.RoutedEventHandler(closeButton_Click);
    }

    void closeButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
    }

    public bool Closeable { get; set; }
    public static readonly DependencyProperty CloseableProperty = DependencyProperty.Register("Closeable", typeof(bool), typeof(IRCTabItem), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


    public List<String> UserList { get; set; }
    public static readonly DependencyProperty UserListProperty = DependencyProperty.Register("UserList", typeof(List<String>), typeof(IRCTabItem), new FrameworkPropertyMetadata(new List<String>(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public String Topic { get; set; }
    public static readonly DependencyProperty TopicProperty = DependencyProperty.Register("Topic", typeof(String), typeof(IRCTabItem), new FrameworkPropertyMetadata("Not Connected", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public bool HasAlerts { get; set; }
    public static readonly DependencyProperty HasAlertsProperty = DependencyProperty.Register("HasAlerts", typeof(bool), typeof(IRCTabItem), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


}

So my questions are:

Am i doing it the right way (best practices)? If so how can i bind DataTemplate to Properties? If not so, what is the correct way of achieve what i am trying to achieve?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about tabcontrol