2

I am building a custom tabitem using template binding. All is fine until I used binding on the margin to a custom value converter, then I get an error in the VS designer: '{DependencyProperty.UnsetValue}' is not a valid value for the 'System.Windows.Controls.Control.Template' property on a Setter.

The converters are as follows:

public class ContentToMarginConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new Thickness(0, 0, -((ContentPresenter)value).ActualHeight, 0);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}
public class ContentToPathConverter :  IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var ps = new PathSegmentCollection(4);
        ContentPresenter cp = (ContentPresenter)value;
        double h = cp.ActualHeight > 10 ? 1.4 * cp.ActualHeight : 10;
        double w = cp.ActualWidth > 10 ? 1.25 * cp.ActualWidth : 10;
        ps.Add(new LineSegment(new Point(1, 0.7 * h), true));
        ps.Add(new BezierSegment(new Point(1, 0.9 * h), new Point(0.1 * h, h), new Point(0.3 * h, h), true));
        ps.Add(new LineSegment(new Point(w, h), true));
        ps.Add(new BezierSegment(new Point(w + 0.6 * h, h), new Point(w + h, 0), new Point(w + h * 1.3, 0), true));
        return ps;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

And the xaml template as follows:

<Style TargetType="TabItem">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TabItem">
                        <Grid Name="grd">                                                   
                            <Path Name="tabPath" 
                                  StrokeThickness="1"
                                  Stroke="{StaticResource ResourceKey=borderBrush}"
                                  Fill="{StaticResource ResourceKey=unselectedBrush}"
                                  Margin="{Binding ElementName=TabItemContent,Converter={StaticResource ResourceKey=content2Margin}}">
                                <Path.Data>
                                    <PathGeometry>
                                        <PathFigure IsClosed="False" StartPoint="1,0"
                                                    Segments="{Binding ElementName=TabItemContent,Converter={StaticResource ResourceKey=content2Path}}">
                                            </PathFigure>        
                                    </PathGeometry>
                                </Path.Data>
                                <Path.LayoutTransform>
                                    <ScaleTransform ScaleY="-1"/>
                                </Path.LayoutTransform>
                            </Path>                                                                                                                
                            <Rectangle Name="TabItemTopBorder" Height="2" Visibility="Visible"
                                   VerticalAlignment="Bottom"
                                   Margin="{Binding ElementName=TabItemContent, Converter={StaticResource ResourceKey=content2Margin}}" />
                            <ContentPresenter Name="TabItemContent" ContentSource="Header"
                                          Margin="10,2,10,2" VerticalAlignment="Center"
                                          TextElement.Foreground="White"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True" SourceName="grd">
                                <Setter Property="Stroke" Value="{StaticResource HoverBrush}" TargetName="tabPath"/>
                            </Trigger>
                            <Trigger Property="Selector.IsSelected" Value="True">
                                <Setter Property="Fill" TargetName="tabPath" Value="{StaticResource ResourceKey=backgroundBrush}"/>                                                                   
                                <Setter Property="Panel.ZIndex" Value="2"/>
                                <Setter Property="Visibility" Value="Hidden" TargetName="TabItemTopBorder"/>
                                <Setter Property="TextElement.Foreground" TargetName="TabItemContent" Value="Black"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Any reason why I get this error in the designer, yet when I build the project is fine????

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
allan
  • 21
  • 1
  • 2
  • It is not uncommon to have issues with the designer, but not at runtime. Is the issue with both of your custom IValueConverters, or just one of them? – CodeNaked Mar 13 '11 at 23:08

3 Answers3

1

I had a similar error too. My converters are defined in a separate assembly. I tried rebuilding the solution, restarting Visual Studio,... the designer still didn't work. But after deleting all the files (including source files and output files), get the source files again and build the solution, the problem fixed.

YantingChen
  • 768
  • 1
  • 12
  • 15
0

I had a similar error. My problem was caused because I had defined in the Style definition using both x:Key="keyName" and x:Name="keyName". By removing the "x:Name='keyName'", solved the problem for me.

Val
  • 1
0

The designer will essentially do a pre compile of what it needs to display your content in the design view.

That may cause issues.

If rebuilding the solution doesn't work, your next best thing is to detect the designer mode and bypass the converter. Essentially having a if(isdesignmode) return; at the top of the Convert method.

More info on how to do that here How to force Visual Studio 2010 to ignore a WPF XAML declared DataContext at design time?

Community
  • 1
  • 1
keyle
  • 2,762
  • 3
  • 24
  • 27