Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4123668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:38:26+00:00 2026-05-20T23:38:26+00:00

I have a TabControl , and I want to do the following: Take first

  • 0

I have a TabControl, and I want to do the following:

  1. Take first and last of the TabItems in TabControl.Items
  2. Get their Margins
  3. Supply those Thicknesses to the converter to convert these two structs into final value

Here is a related code showing what I am trying to do:

<Border.Padding>
    <MultiBinding Converter="{StaticResource MarginsToPaddingConverter}">
        <Binding Path="Margin">
            <Binding.Source>
                <Binding RelativeSource="{RelativeSource AncestorType=TabControl}" Path="Items" Converter="{StaticResource ItemCollectionToFirstItemConverter}" ConverterParameter="{x:Type TabItem}" />
            </Binding.Source>
        </Binding>
        <Binding Path="Margin">
            <Binding.Source>
                <Binding RelativeSource="{RelativeSource AncestorType=TabControl}" Path="Items" Converter="{StaticResource ItemCollectionToLastItemConverter}" ConverterParameter="{x:Type TabItem}" />
            </Binding.Source>
        </Binding>
    </MultiBinding>
</Border.Padding>

But I can’t set Binding as RelativeSource or Source of other Binding. Basically the solution at hand is to create converter, which would take TabControl.Items and convert it to the final value, but the problem is I want to animate Margins of both TabItems, so I need to bind specifically to these properties. If I would bind to TabControl.Items, the Border.Padding would not get refreshed if Margin of any TabItem would change. So what should I do?

Update

Ok, so one of the possible solutions is to hook into TabItem.Loaded event, and then use DependencyPropertyDescriptor to hook Changed event on appropriate properties, then hook all items in TabItem.Items collection, hook any new items and automatically unhook all old items, and hook like million other stuff. But this is quite complicated and it’s like 400 LOC. Isn’t there anything simpler? Preferably in pure XAML.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:38:27+00:00Added an answer on May 20, 2026 at 11:38 pm

    Unfortunately this modified response is not as elegant as I’d like, but it should work.

    Basically, use another dummy control as a relay for the binding. Specifically, this scenario arises when you want to pull the first and last of a collection. You can’t just use a converter to grab the first/last and the property all at once, because if you change a property on the first or last item, the converter won’t pick up the change. So you have to do something that melds in with dependency properties – almost like some sort of a second order dependency property would be nice.

    Anyways, here’s some code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows;
    using System.Collections;
    
    namespace WpfApplication1
    {
        public class CollectionHelper : Control
        {
            public static DependencyProperty CollectionProperty = DependencyProperty.Register(
                "Collection",
                typeof(IEnumerable),
                typeof(CollectionHelper),
                new FrameworkPropertyMetadata(OnCollectionChanged));
    
            public IEnumerable Collection
            {
                get { return GetValue(CollectionProperty) as IEnumerable; }
                set { SetValue(CollectionProperty, value); }
            }
    
            private static void OnCollectionChanged(object rawSender, DependencyPropertyChangedEventArgs args)
            {
                CollectionHelper sender = (CollectionHelper)rawSender;
                IEnumerable value = args.NewValue as IEnumerable;
                if(value==null)
                {
                    sender.First = null;
                    sender.Last = null;
                    return;
                }
                bool isFirstSet = false;
                object lastItemTemp = null;
                foreach (var item in value)
                {
                    if (!isFirstSet)
                    {
                        sender.First = item;
                        isFirstSet = true;
                    }
                    lastItemTemp = item;
                }
                if (!isFirstSet)
                    sender.First = null;
                sender.Last = lastItemTemp;
            }
    
            public DependencyProperty FirstProperty = DependencyProperty.Register(
                "First",
                typeof(object),
                typeof(CollectionHelper));
    
            public object First
            {
                get { return GetValue(FirstProperty); }
                set { SetValue(FirstProperty, value); }
            }
    
            public DependencyProperty LastProperty = DependencyProperty.Register(
                "Last",
                typeof(object),
                typeof(CollectionHelper));
    
            public object Last
            {
                get { return GetValue(LastProperty); }
                set { SetValue(LastProperty, value); }
            }
        }
    }
    

    And an actual use case:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:Helper="clr-namespace:WpfApplication1"
            Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <Helper:CollectionHelper x:Name="Helper" Collection="{Binding SomeStrings}" />
            <TextBlock Text="{Binding ElementName=Helper, Path=First}" />
            <TextBlock Text="{Binding ElementName=Helper, Path=Last}" />
        </StackPanel>
    </Window>
    

    So the idea here would be to grab the first and last item using the relay control and then bind to there margins.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF TabControl, with some TabItems. I want margins on the left
I have a form with a tabcontrol and 4 tabs. I want to open
I have a TabControl with templated content as below: <TabControl x:Name=Items SelectedItem={Binding ActiveItem} TabStripPlacement=Left
I'm trying to do the following thing: I have a TabControl with several tabs.
I have following situation <stackpanel > <ViewBox height=25/ > <tabcontrol> <tabitem> <Canvas /> </tabitem>
I have the following class, and want to pass the text variable as RoutedEventArgs.
I have a TabControl in WPF. I want to find an event that occurs
I have a TabControl that allows users to manage documents such as the following:
Customizing TabItem I have a TabControl bound to my ViewModel i also want to
I have a TabControl in my WPF application. I want my application to basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.