0

I am trying to make a zoomable toolbar which contains, besides other elements, a ComboBox.

I have a named ComboBox as the content of an instance of a local:ZoomableStackPanel custom control class that inherits from StackPanel and adds a ZoomFactor property which is specified in the IZoomableControl interface. There is a compiler error.

The error seems to be caused by the custom control implementation, but I do not know what is missing. If I replace local:ZoomableStackPanel with a normal StackPanel, there isn't any error. I also tried without any IsSelected ComboBoxItem-s, and the error remains.

MainWindow.xaml

<Window x:Class="cs_wpf_test_14.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:cs_wpf_test_14"
        mc:Ignorable="d" Height="60" Width="200">
    <local:ZoomableStackPanel Orientation="Horizontal">
        <ComboBox Name="MyComboBox">
            <ComboBoxItem IsSelected="True">abc</ComboBoxItem>
            <ComboBoxItem>def</ComboBoxItem>
            <ComboBoxItem>ghi</ComboBoxItem>
        </ComboBox>
    </local:ZoomableStackPanel>
</Window>

ZoomableStackPanel

XAML

<StackPanel x:Class="cs_wpf_test_14.ZoomableStackPanel"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:cs_wpf_test_14"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <StackPanel.LayoutTransform>
        <ScaleTransform
                        ScaleX="{Binding
                        RelativeSource={RelativeSource Mode=FindAncestor,
                            AncestorType=StackPanel},Path=ZoomFactor}"
                        ScaleY="{Binding
                        RelativeSource={RelativeSource Mode=FindAncestor,
                            AncestorType=StackPanel},Path=ZoomFactor}">
        </ScaleTransform>
    </StackPanel.LayoutTransform>
</StackPanel>

Code-behind

/// <summary>
/// Interaction logic for ZoomableStackPanel.xaml
/// </summary>
public partial class ZoomableStackPanel : StackPanel, IZoomableControl
{
    public ZoomableStackPanel()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty ZoomFactorProperty =
        DependencyProperty.Register("ZoomFactor", typeof(decimal), typeof(ZoomableStackPanel),
            new FrameworkPropertyMetadata(1M));
    public decimal ZoomFactor
    {
        get
        {
            return (decimal)GetValue(ZoomFactorProperty);
        }
        set
        {
            SetValue(ZoomFactorProperty, value);
        }
    }
}

The interface

public interface IZoomableControl
{
    decimal ZoomFactor { get; set; }
}

I expected the test project to build successfully but there is this compiler error:

Cannot set Name attribute value 'MyComboBox' on element 'ComboBox'. 'ComboBox' is under the scope of element 'ZoomableStackPanel', which already had a name registered when it was defined in another scope.

silviubogan
  • 3,343
  • 3
  • 31
  • 57
  • i usually use `x:Name` not `Name`. I cant recall the technical reasons why. I remember when i first started WPF that is the preferred tag for naming controls. Not sure if that will help. – Ginger Ninja May 30 '19 at 14:28
  • Thank you for the advice. I tried with x:Name and it still does not work. – silviubogan May 30 '19 at 15:41
  • Maybe this helps? https://stackoverflow.com/questions/751325/how-to-create-a-wpf-usercontrol-with-named-content – mami May 30 '19 at 20:24

0 Answers0