选择 tabitem 时设置图像 [英] Set an image when a tabitem is selected
本文介绍了选择 tabitem 时设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是 wpf 的新手,无法弄清楚在选择选项卡项时应该如何设置图像.
I am new with the wpf and I cannot figure out how should I set an image when a tab item is selected.
我写了一种风格,但它不起作用.它向我展示了任何东西...
I write a style but it doesnt work. It shows me anything...
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid>
<StackPanel Orientation="Horizontal">
<HeaderedItemsControl Name="img" >
</HeaderedItemsControl>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Visibility" TargetName="img" Value="Visible"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Visibility" TargetName="img" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
对于标签控件我写了代码:
For the tab control I wrote the code:
<TabControl Name="tabControl1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Height="311"
Width="503">
<TabItem Name="tabItem1" Background="Red" >
<TabItem.Header>
<Grid>
<TextBlock Text=" First" Width="100" Margin="-2,33,4,16" />
<Image Name="img" Source="menu.png" Height="20" Margin="0,60,0,0" />
</Grid>
</TabItem.Header>
<Grid>
<TextBox Name="denumire" Margin="31,20,169,67" Height="20" Width="60"/>
</Grid>
</TabItem>
<TabItem Name="tabItem2" Header="tabItem2" Background="Red">
</TabItem>
</TabControl>
推荐答案
按照您的风格,将 HeaderedItemsControl
更改为如下所示:
In your style, change the HeaderedItemsControl
to something like this:
<HeaderedItemsControl>
<TextBlock Text="{TemplateBinding Header}" Width="100"/>
<Image x:Name="img" Source="menu.png" Height="20" />
</HeaderedItemsControl>
并删除 TabControl
中的整个
部分.
and remove the entire <TabItem.Header>
section in your TabControl
.
这篇关于选择 tabitem 时设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文