Click here to Skip to main content
15,036,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good afternoon to all!

I have the next trouble in my WPF app.

I have a TabControl and with a Converter, I achieve to resize the width of the Tabitems . This Is working fine.

Now, in my app, I want have a Button to remove some of these TabItems and at the same time resize the rest of the TabItems. Whenever I remove one of the included TabItems, an error is taking place in my converter (Tabcontrol on the code below is null). What am I doing wrong?


My converter is like this:

C#
public class TabSizeConverterVertical : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            TabControl tabControl = values[0] as TabControl;
            double height = tabControl.ActualHeight / tabControl.Items.Count;
            //Subtract 1, otherwise we could overflow to two rows.
            return (height <= 1) ? 0 : (height);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }


and I use this converter in a style defined for TabItem (below)


XML
<Setter Property="Height">
       <Setter.Value>
           <MultiBinding Converter="{StaticResource tabSizeVertical}">
               <Binding RelativeSource="{RelativeSource Mode=FindAncestor,
           AncestorType={x:Type TabControl}}" />
               <Binding RelativeSource="{RelativeSource Mode=FindAncestor,
           AncestorType={x:Type TabControl}}" Path="ActualHeight" />
           </MultiBinding>
       </Setter.Value>
   </Setter>

Thanks for all.
Posted
Updated 24-Oct-13 22:28pm
v4
Comments
Pheonyx 24-Oct-13 17:35pm
   
What error? Have you tried to debug it at all?
torin86 25-Oct-13 2:28am
   
Sorry. I forgot the error!!
When I click the button, the tabItem is removed but TabControl doesn´t resize again. how can I force to apply the converter after remove TabItem?

My converter is like this:

public class TabSizeConverterVertical : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
TabControl tabControl = values[0] as TabControl;
double height = tabControl.ActualHeight / tabControl.Items.Count;
//Subtract 1, otherwise we could overflow to two rows.
return (height <= 1) ? 0 : (height);
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter,
System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}

Thanks.
Pheonyx 25-Oct-13 2:46am
   
Thanks for the code, but you still haven't said what error occurs, or on what line.
Also, where in your XAML is the converter used? Could you post the small section of XAML that uses it please. Use the "Improve Question" button to add your code to the question itself as well as the error message you get. When you click Improve Question you should see how I added your code from your comment into your question for you using "<pre></pre>" tags. Do the same with your XAML code.

1 solution

first you do "clean solution"
then run the project it will successful

enjoy
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900