动态创建的wpf控件与它所基于的控件的工作方式不同

时间:2013-06-29 09:34:42

标签: wpf visual-studio-2010 controls

继续我之前的问题create dynamically complex controls in wpf 我的代码基于可移动和可调整大小的形状的代码项目article

最初我创建了一个可在我的xaml中调整大小的控件,并且在代码中创建控件的副本时出现问题。 现在,这个问题已经解决了,但我已经添加了移动我的可调整大小控件的功能,它完美地工作,但是在代码中创建的副本只会调整大小而不会移动,我不知道为什么会这样。 / p>

这是我的xaml:

<Window x:Class="MazeBuilder.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:MazeBuilder"
    Title="MainWindow" Height="480" Width="640">
<Window.Resources>

    <!-- MoveThumb Template -->
    <ControlTemplate x:Key="MoveThumbTemplate" TargetType="{x:Type s:MoveThumb}">
        <Rectangle Fill="Transparent"/>
    </ControlTemplate>

    <!-- ResizeDecorator Template -->
    <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
        <Grid>
            <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
                   VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
            <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
                   VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
            <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
                   VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
            <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
                   VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
            <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
                   VerticalAlignment="Top" HorizontalAlignment="Left"/>
            <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
                   VerticalAlignment="Top" HorizontalAlignment="Right"/>
            <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
                   VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
            <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
                   VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
        </Grid>
    </ControlTemplate>

    <!-- Designer Item Template-->
    <ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl">
        <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
            <Control Template="{StaticResource ResizeDecoratorTemplate}"/>
            <s:MoveThumb Template="{StaticResource MoveThumbTemplate}" Cursor="SizeAll"/>                
            <ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
        </Grid>
    </ControlTemplate>

</Window.Resources>
    <Canvas x:Name="LayoutRoot" MouseDown="LayoutRoot_MouseDown" MouseMove="LayoutRoot_MouseMove">
    <Popup Name="PopupEsales" Placement="Right" IsEnabled="True" IsOpen="False" Grid.RowSpan="2">
        <ListView Height="145" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lvSalesPersonIdSearch" VerticalAlignment="Top" Width="257" >
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Sales Persons Id" Width="100" DisplayMemberBinding="{Binding Path=SPID}" />
                    <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Path=Name}" />
                </GridView>
            </ListView.View>
        </ListView>
    </Popup>
    <Menu Height="23" IsMainMenu="True" HorizontalAlignment="Left"  Name="menu1" VerticalAlignment="Top" Width="640">
        <MenuItem Header="File">
            <MenuItem Header="New Maze"  Click="New_Click" />
            <MenuItem Header="Load Maze" Click="Load_Click" />
            <MenuItem Header="Save Maze" Click="Save_Click"  />

        </MenuItem>
        <MenuItem Header="Tools">
            <MenuItem Header="Show"  Click="ShowTools_Click" />
        </MenuItem>
    </Menu>

    <ContentControl  Width="130"
                MinWidth="50"
                Height="130"
                MinHeight="50"
                Canvas.Top="150"
                Canvas.Left="470"
                Template="{StaticResource DesignerItemTemplate}">
        <Rectangle Fill="Blue"
           IsHitTestVisible="False"/>
    </ContentControl>


    <Canvas.Background>
        <SolidColorBrush Color="White" Opacity="0"/>
    </Canvas.Background>


</Canvas>

这是我试图复制的控件:

            <ContentControl  Width="130"
            MinWidth="50"
            Height="130"
            MinHeight="50"
            Canvas.Top="150"
            Canvas.Left="470"
            Template="{StaticResource DesignerItemTemplate}">
    <Rectangle Fill="Blue"
       IsHitTestVisible="False"/>
</ContentControl>

这是我的代码,用于创建控件的新实例:

   ContentControl cc = new ContentControl();
    ControlTemplate ct = new ControlTemplate();
    object rs = this.Resources["DesignerItemTemplate"];
    ct = (ControlTemplate)rs;
    cc.Template = ct;
    cc.Height = 10;
    cc.Width = 10;
    cc.Content = new Rectangle { Fill = new SolidColorBrush(Color.FromRgb(0,0,255)) };
    LayoutRoot.Children.Add(cc);
    Canvas.SetLeft(cc, 300);
    Canvas.SetTop(cc, 300);

这会创建一个可调整大小的控件,但它根本不会移动,并且当鼠标悬停在光标上时光标不会像在xaml中创建的那样更改。我相信我已经分配了正确的资源,我不明白为什么这不起作用。

1 个答案:

答案 0 :(得分:1)

您未在IsHiteTestVisible = false上设置Rectangle。这就是问题所在,因为您的ContentPresenter位于模板中的MoveThumb之上。我不确定MoveThumb的样子,但如果它只是一个透明的矩形,我会把它放在ContentPresenter的顶部,这样你就不用担心设置IsHitTestVisible了。 1}}关于你添加的所有孩子。