我怎么能在代码背后修改Controltemplate WPF / C#?

时间:2014-07-13 18:57:19

标签: wpf

对不起我糟糕的英语:

在我的应用程序中,我有一个ResourceDictionary“DesignerItem”,我在其中定义一个控制模板“ControlTemplate x:Key =”ConnectorDecoratorTemplate“TargetType =”{x:Type Control}“”,这个控制模板允许我在对象上有点“DesignerItem”类型的这一点允许在点之间绘制线条。我的ResourceDictionary定义为:

<!-- Connector Style -->
<!-- les Connector son les points qui nous permet des tracer les lignes-->
<Style TargetType="{x:Type s:Connector}">
    <Setter Property="Width" Value="8"/>
    <Setter Property="Height" Value="8"/>
    <Setter Property="Cursor" Value="Cross"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type s:Connector}">
                <Grid >
                    <!-- transparent extra space makes connector easier to hit -->
                    <!--c un carrer qui nous permet de dessiner la line entre les object-->
                    <Rectangle Fill="red" Margin="-2"/>
                    <Rectangle Fill="Red" StrokeThickness="5" Stroke="#AA000080"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>




<!--
la partie suivante c'est la partie qui me me pose un probleme,
c'est la partie que je veux la modifer a partir du code source 
-->

<!-- ConnectorDecoratorTemplate Default Template -->


<ControlTemplate  x:Key="ConnectorDecoratorTemplate" TargetType="{x:Type Control}">

    <Grid x:Name="LesPointDeConnexion"><!-- les point qui me permet de tracer des lignes-->
            <Grid.RowDefinitions>
                <RowDefinition Height="0.5*"/>
                <RowDefinition Height="0.5*"/>
            </Grid.RowDefinitions>
        <s:Connector  Orientation="Left"  VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="0"/>
        <s:Connector  Orientation="Left"  VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5" Grid.Row="1"/>

        </Grid>
</ControlTemplate>




<!-- DesignerItem Style -->
<Style  TargetType="{x:Type s:DesignerItem}">
    <Setter Property="MinWidth" Value="10"/>
    <Setter Property="MinHeight" Value="10"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>        
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type s:DesignerItem}">
                <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">


                    <!-- PART_DragThumb -->
                    <c:DragThumb x:Name="PART_DragThumb" 
                                 Cursor="SizeAll"/>

                    <!-- PART_ResizeDecorator -->
                    <Control x:Name="PART_ResizeDecorator" 
                             Visibility="Collapsed"
                             Template="{StaticResource ResizeDecoratorTemplate}"/>


                    <!-- PART_ContentPresenter -->
                    <ContentPresenter x:Name="PART_ContentPresenter"
                                      HorizontalAlignment="Stretch"
                                      VerticalAlignment="Stretch"
                                      Content="{TemplateBinding ContentControl.Content}"
                                      Margin="{TemplateBinding ContentControl.Padding}"/>



                    <!-- PART_ConnectorDecorator -->
                    <Control x:Name="PART_ConnectorDecorator"
                             Visibility="Visible"
                             Template="{StaticResource ConnectorDecoratorTemplate}"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsGroup}">
                        <Setter TargetName="PART_DragThumb" Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}"/>
                            <Condition Value="{x:Static sys:Guid.Empty}" Binding="{Binding RelativeSource={RelativeSource Self},Path=ParentID}"/>
                        </MultiDataTrigger.Conditions>
                        <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
                    </MultiDataTrigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsDragConnectionOver}">
                        <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                    <DataTrigger Value="True" Binding="{Binding RelativeSource={RelativeSource Self},Path=IsGroup}">
                        <Setter TargetName="PART_ConnectorDecorator" Property="Visibility" Value="Hidden"/>
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

现在我想在后面的代码中定义或修改控制模板“ControlTemplate x:Key =”ConnectorDecoratorTemplate“TargetType =”{x:Type Control}“”,因为我的控件模板是动态的(对于某些对象,我有一个DesigneItem) 2分,其他我有4,5,6或更多)因此我想在后面的代码中创建或修改这个控件模板。

请帮助我:(

0 个答案:

没有答案