在控件模板中绑定

时间:2015-03-16 18:10:58

标签: wpf

我定义了以下模板。如果在StackPanel之外,TextBlock PART_TextBlock正确绑定,但是当放置在StackPanel内部时,我会收到绑定错误。 StackPanel datacontext绑定到转换器,并根据需要应用故事板(闪烁选项卡)。我需要选项卡以及文本显示在标题页中,文本背景中闪烁。

                <dashboard:EditableTabHeaderControl.Template>
                    <ControlTemplate TargetType="{x:Type dashboard:EditableTabHeaderControl}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <StackPanel  DataContext="{Binding Path=., Mode=OneWay, Converter={StaticResource DependencyObjectToFilterConverter}}" Grid.Row="1" Background="Transparent" >
                                <StackPanel.Style>
                                    <Style TargetType="{x:Type StackPanel}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=HasError}" Value="True" >
                                                <DataTrigger.EnterActions>
                                                    <BeginStoryboard Name="StartBlinking" >
                                                        <Storyboard>
                                                            <ColorAnimation 
                                                      Storyboard.TargetProperty="Background.Color"

                                                      To="Red"
                                                      Duration="00:00:00.4"
                                                      RepeatBehavior="Forever"
                                                      AutoReverse="True"  />
                                                        </Storyboard>
                                                    </BeginStoryboard>
                                                </DataTrigger.EnterActions>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=HasError}" Value="False" >
                                                <DataTrigger.EnterActions>
                                                    <RemoveStoryboard BeginStoryboardName="StartBlinking" />
                                                </DataTrigger.EnterActions>

                                            </DataTrigger>

                                        </Style.Triggers>

                                    </Style>

                                </StackPanel.Style>

                                <TextBlock Text=" " VerticalAlignment="Top">
                                </TextBlock>
                                <TextBox x:Name="PART_TabHeader" Text="{Binding Path=Title,
                                    Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" Visibility="Collapsed">
                                </TextBox>
                                <TextBlock x:Name="PART_TextBlock" Text="{Binding Path=Title,
                                    Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}" >
                                </TextBlock>
                            </StackPanel>

                            <!-- IF THE TEXBOX/TEXTBLOCK ARE PLACED HERE TITLE BINDS CORRECTLY-->

                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsInEditMode" Value="True">
                                <Trigger.Setters>
                                    <Setter TargetName="PART_TabHeader" Property="Visibility" Value="Visible"/>
                                    <Setter TargetName="PART_TextBlock" Property="Visibility" Value="Collapsed"/>
                                </Trigger.Setters>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </dashboard:EditableTabHeaderControl.Template>
            </dashboard:EditableTabHeaderControl>

1 个答案:

答案 0 :(得分:0)

我终于想出了Binding表达式,以下工作

Text =&#34; {Binding DataContext.Title,RelativeSource = {RelativeSource AncestorType = dashboard:EditableTabHeaderControl}}&#34;