在后续事件中定义模板中控件的名称

时间:2013-11-18 07:24:45

标签: c# wpf templates

我正在尝试访问Img_ToolKitOpening事件中的WebBrowser“wb”但无法找到它。 Wb在模板中定义

    <Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Padding" Value="2,0,0,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border  x:Name="Bd" SnapsToDevicePixels="true"  Background="#EEFFFFFF" BorderBrush="#FFCCCCCC"  RenderTransformOrigin="1,1" 
                            HorizontalAlignment="Center" VerticalAlignment="Center"
                            BorderThickness="1">
                        <Grid>

                           <Image x:Name="img" ToolTipService.Placement="Top"
                            Source="{Binding Path=ImageUri}" Height="64" 
                             Stretch="Fill" Width="64" ToolTipOpening="img_ToolTipOpening">
                                <Image.RenderTransform>
                                    <TransformGroup>
                                        <ScaleTransform ScaleX="1" ScaleY="1" x:Name="scaleTrans"/>
                                    </TransformGroup>
                                </Image.RenderTransform>
                                <Image.ToolTip>
                                    <ToolTip BorderBrush="{x:Null}" Background="{x:Null}" Effect="{x:Null}"
                                             DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" 
                                             HasDropShadow="False">
                                        <Border Background="{x:Null}" VerticalAlignment="Center" Height="160" Margin="0" Width="100" 
                                                HorizontalAlignment="Center">
                                            <Grid Background="{x:Null}">
                                                <WebBrowser x:Name="wb"  
                                                 Width="600" Height="600"   />
                                            </Grid>
                                        </Border>
                                    </ToolTip>
                                </Image.ToolTip>
                            </Image> 
                        </Grid>
                      </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ItemsPanelTemplate x:Key="ItemsPanelTemplate1">
        <VirtualizingStackPanel IsItemsHost="True" Orientation="Horizontal"/>
    </ItemsPanelTemplate>


</Window.Resources>
<Grid>
    <ListBox x:Name="lbMovies" Height="92" Margin="81,0,94,26" ItemsPanel="{DynamicResource ItemsPanelTemplate1}"
             ItemContainerStyle="{DynamicResource ListBoxItemStyle}" ItemsSource="{Binding}"
             VerticalAlignment="Bottom" />
</Grid>

我希望能够执行wb.Navigate(@“c:\ dump \ bb.htm”)来定义 显示工具提示时显示的HTML页面。

1 个答案:

答案 0 :(得分:0)

如果您无法设法在代码隐藏中访问WB,那么在模板中定义的Ifits可以尝试绑定到Source,然后在更新源时绑定事件。

Source="{Binding Path=WebBrowserUri}" SourceUpdated="wb_SourceUpdated"