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.