XAML以样式绑定到Element

时间:2014-12-02 07:57:27

标签: c# wpf xaml

我正在向EntityControl添加EdgeControlEdgeControl包含名为PART_edgePath的元素。我试图从EntityControl

中绑定到这个元素
<UserControl x:Class="UI.Graph.EntityControl" ... >

<Ellipse Width="15" Height="15"  Fill="Blue">
    <Ellipse.RenderTransform>
        <TransformGroup>
            <TranslateTransform X="-7.5" Y="-7.5" />
            <!-- use radius to shift circle to path -->
            <TranslateTransform x:Name="AnimatedTranslateTransform"/>
        </TransformGroup>
    </Ellipse.RenderTransform>
    <Ellipse.Triggers>
        <EventTrigger RoutedEvent="Binding.TargetUpdated">
            <BeginStoryboard>
                <Storyboard RepeatBehavior="Forever" >
                    <DoubleAnimationUsingPath
                                            Source="X" Duration="{Binding Path=Duration}"
                                            Storyboard.TargetProperty="X" 
                                            Storyboard.TargetName="AnimatedTranslateTransform"
                                            PathGeometry="{Binding ElementName=PART_edgePath, Path=Data, Mode=OneWay, NotifyOnTargetUpdated=True, Converter={StaticResource GeometryToPathGeometry}}"/>
                    <DoubleAnimationUsingPath
                                            Source="Y" Duration="{Binding Path=Duration}"
                                            Storyboard.TargetProperty="Y"
                                            Storyboard.TargetName="AnimatedTranslateTransform"
                                            PathGeometry="{Binding ElementName=PART_edgePath, Path=Data, Mode=OneWay, NotifyOnTargetUpdated=True, Converter={StaticResource GeometryToPathGeometry}}"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Ellipse.Triggers>
</Ellipse>

</UserControl>

源元素位于Style中的ContentTemplate中。之前我在Style中有EntityControl时也很好用:

<ResourceDictionary ... >

<!-- EDGE CONTROL -->
<Style TargetType="{x:Type gxl:EdgeControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type gxl:EdgeControl}">
                <Canvas x:Name="EdgeCanvas">
                    <Path Stroke="{TemplateBinding Foreground}"
                      StrokeThickness="2" MinWidth="1" MinHeight="1"
                      ToolTip="{TemplateBinding ToolTip}"
                        x:Name="PART_edgePath"/>
                    <Path Stroke="{TemplateBinding Foreground}"
                      StrokeThickness="2" MinWidth="1" MinHeight="1" 
                        x:Name="PART_edgeArrowPath"/>


                    <!-- HERE the EntityControl was part of the style -->
                    <!--<Ellipse ... />-->

                </Canvas>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
</ResourceDictionary>

我以编程方式在代码中创建EntityControl

EntityControl entity = new EntityControl();
Canvas canvas = VisualTreeHelper.GetChild(edgeControl, 0) as Canvas;
canvas.Children.Add(entity);
entity.InitializeComponent();
entity.UpdateLayout();

不幸的是DoubleAnimationUsingPath.PathGeometry的{​​{1}}为空。 我试了很多东西让它工作但失败了。关于为什么这不起作用以及我如何/应该如何工作的一些澄清将受到高度赞赏!

谢谢!

0 个答案:

没有答案