我有一个数据绑定标签控件,其中包含n
个标签页。我有标签控件的标题和内容模板。标题模板包含带有图片和TextBlock
的堆栈面板,内容模板包含WebBrowser
控件。
我被要求做的是在浏览器浏览时动画(主要是淡入/淡出)图像。我可以在后面的代码中创建和执行故事板,但我确实如何根据浏览的浏览器访问标题模板。有没有办法做到这一点?或者可能是从xaml中的事件触发器执行此操作的方法吗?
编辑:添加了XAML代码。除了'tabitem'类和集合片以及我用来绑定到'webbrowser'控件的source属性的'webbrowser'实用程序类之外,目前还没有真正的代码。我还没有创建“故事板”,因为我不确定如何去做。 “HeaderTemplate”数据模板中的图像是我需要设置动画的图像。我只是通过集合中的一些随机选项卡项,通常从数据库中获取该信息。
XAML:
<Window...>
<Window.Resources>
<src:ImageSourceConverter x:Key="ImageSourceConverter"/>
<ObjectDataProvider x:Key="TabListResource" ObjectType="{x:Type src:TabList}" />
<DataTemplate x:Key="HeaderTemplate">
<StackPanel Orientation="Horizontal" >
<Image Source="{Binding Path=ImageSource, Converter={StaticResource ImageSourceConverter}}" Height="16" Width="16" x:Name="imgHeader" />
<TextBlock Text="{Binding Path=Header}" Padding="5,0,0,0" x:Name="Header" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="ContentTemplate">
<WebBrowser src:WebBrowserUtility.BindableSource="{Binding Content}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Name="webBrowser" Navigated="webBrowser_Navigated" Navigating="webBrowser_Navigating"/>
</DataTemplate>
</Window.Resources>
<DockPanel Name="dockMain">
<TabControl ItemsSource="{Binding Source={StaticResource TabListResource}}"
ItemTemplate="{StaticResource HeaderTemplate}"
ContentTemplate="{StaticResource ContentTemplate}">
<TabControl.Template>
<ControlTemplate TargetType="TabControl">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Hidden" >
<TabPanel x:Name="HeaderPanel"
Panel.ZIndex ="1"
KeyboardNavigation.TabIndex="1"
Grid.Column="0"
Grid.Row="0"
Margin="2,2,2,0"
IsItemsHost="true" />
</ScrollViewer>
<ContentPresenter x:Name="PART_SelectedContentHost"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Margin="{TemplateBinding Padding}"
ContentSource="SelectedContent" Grid.Row="1"/>
</Grid>
</ControlTemplate>
</TabControl.Template>
</TabControl>
</DockPanel>
</Window>
TabItemData:
class TabItemData
{
private string _header;
private string _content;
private string _imageSource;
public TabItemData(string header, string content, string imageSource)
{
_header = header;
_content = content;
_imageSource = String.Format(@"../Images/{0}",imageSource);
}
public string Header
{
get { return _header; }
}
public string Content
{
get { return _content; }
}
public string ImageSource
{
get { return _imageSource; }
}
}
TabItem Collection:
class TabList : ObservableCollection<TabItemData>
{
public TabList() : base()
{
Add(new TabItemData("Header 1", "url1", "img1.png"));
Add(new TabItemData("Header 2", "url2", "img2.png"));
Add(new TabItemData("Header 3", "url3", "img3.png"));
}
}
Webbrowser工具:用于绑定到源属性
public static class WebBrowserUtility
{
public static readonly DependencyProperty BindableSourceProperty =
DependencyProperty.RegisterAttached("BindableSource", typeof(string), typeof(WebBrowserUtility), new UIPropertyMetadata(null, BindableSourcePropertyChanged));
public static string GetBindableSource(DependencyObject obj)
{
return (string)obj.GetValue(BindableSourceProperty);
}
public static void SetBindableSource(DependencyObject obj, string value)
{
obj.SetValue(BindableSourceProperty, value);
}
public static void BindableSourcePropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
WebBrowser browser = o as WebBrowser;
if (browser != null)
{
string uri = e.NewValue as string;
browser.Source = !String.IsNullOrEmpty(uri) ? new Uri(uri) : null;
}
}
}