选择选项卡时设置图像
我是 wpf 新手,我不知道在选择选项卡项时应该如何设置图像。
我写了一个样式,但它不起作用。它向我展示了任何东西......
<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>
对于选项卡控件,我编写了代码:
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的样式中,将
HeaderedItemsControl
更改为如下所示:并删除
TabControl
中的整个
部分。In your style, change the
HeaderedItemsControl
to something like this:and remove the entire
<TabItem.Header>
section in yourTabControl
.