【发布时间】:2014-08-22 10:07:23
【问题描述】:
我创建了一个ImageButton 用户控件。我想要另一个用户控件 - TabItem - 使用 ImageButton 但 TabItem 不会显示图像。
我需要做什么才能让TabItem 显示ImageButton 的图像?
这是我的代码:
<!--MainPage.xaml-->
<Page
x:Class="ButtonTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ButtonTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:buttons="using:ButtonTest" Background="Transparent">
<StackPanel>
<!--there is an image displayed for this-->
<buttons:ImageButton ImageSource="overview_64.png" />
<!--but not for this-->
<buttons:TabItem ImageSource="overview_64.png" TabItemText="button text" />
</StackPanel>
</Page>
<!--ImageButton.xaml-->
<UserControl
x:Class="ButtonTest.ImageButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ButtonTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="100"
d:DesignWidth="100">
<Button Name="imgButton" Style="{StaticResource imageButton}" />
</UserControl>
public sealed partial class ImageButton : UserControl
{
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("ImageSource", typeof(string), typeof(ImageButton), null);
public ImageButton()
{
this.InitializeComponent();
this.DataContext = this;
}
public string ImageSource
{
get { return (string)GetValue(ImageProperty); }
//set { SetValue(ImageProperty, value); }
set { SetValue(ImageProperty, "/Resources/Images/" + value); }
}
}
这是 TabItem.xaml,它使用 ImageButton。 TabItem 没有显示图像。我是否正确绑定了 ImageSource?
<!--TabItem.xaml-->
<UserControl x:Class="ButtonTest.TabItem"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:btn="using:ButtonTest"
d:DesignHeight="85" d:DesignWidth="80">
<StackPanel Orientation="{Binding TabOrientation}">
<!-- TabItem uses ImageButton -->
<btn:ImageButton ImageSource="{Binding ImageSource}" />
<TextBlock Name="txtText" Text="{Binding TabItemText}" />
</StackPanel>
</UserControl>
public sealed partial class TabItem : UserControl
{
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("TabItemText", typeof(string), typeof(TabItem), null);
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("ImageSource", typeof(string), typeof(TabItem), null);
public string TabItemText
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public string ImageSource
{
get { return (string)GetValue(ImageProperty); }
set { SetValue(ImageProperty, "/Resources/Images/" + value); }
}
public TabItem()
{
this.InitializeComponent();
this.DataContext = this;
}
}
ImageButton 使用的样式在这里定义:
<!--Image Button Style is defined here-->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HKC.Winterfell.WP81.Resources.Styles.Buttons">
<Style x:Key="imageButton" TargetType="Button">
<Setter Property="MinWidth" Value="50"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Border x:Name="border" Background="Transparent" BorderBrush="Transparent" BorderThickness="2" CornerRadius="20">
<Grid>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Width="Auto" Grid.RowSpan="2"/>
</Grid>
</Border>
<Image x:Name="ImgShadow" Source="/Resources/Images/shadow_64.png" Visibility="Visible" Height="67" Width="67" Margin="10,8,0,0" />
<Image x:Name="Img" Source="{Binding ImageSource}" Height="64" Width="64" Margin="0,0,0,0" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="MouseOver"/>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="ImgShadow">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="Img">
<DiscreteObjectKeyFrame KeyTime="0" Value="10,8,0,0" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
【问题讨论】:
标签: c# xaml windows-runtime windows-phone windows-phone-8.1