我创建了一个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>
答案 0 :(得分:2)
我不认为从控件中更改控件的DataContext是个好主意。我已经看过几次而且总是引起了一些问题。
潜在问题
我认为这是您案件中的问题。从TabItem的xaml:
中查看这一行<btn:ImageButton ImageSource="{Binding ImageSource}" />
此绑定将ImageButton的ImageSource属性绑定到ImageButton的DataContext(正如所有绑定一样 - 它们绑定到设置它们的控件的DataContext)。但是在构造函数中,您将该上下文更改为ImageButton本身,因此该属性基本上与自身绑定。
轻松修复
最简单的解决方法是更改其内容的DataContext,而不是更改整个控件的DataContext。在您的情况下,这将是ImageButton的Button元素的DataContext。
换句话说 - 在ImageButton的构造函数中更改此行:
this.DataContext = this;
到
this.imgButton.DataContext = this;
当然,对TabItem控件也一样。
结论或某事......
基本上,改变DataContext是一个坏主意(即使你按照我的建议改变它),所以尽量避免使用它。
P.S。抱歉,我可能会给你一个没有DC更改的解决方案,但我现在太忙了。如果其他人这样做,我会投票给他们! :)
编辑 - 其他观察(与问题无关)
我也不认为setter是个好主意:
set { SetValue(ImageProperty, "/Resources/Images/" + value); }
如果您需要使用不在Resources / Images中的图像,您会非常难过。此外,将属性的类型更改为ImageSource(因为它通常用于图像源:)或对象可能是个好主意。在WinRT中,很多东西都可以转换为ImageSource,因此如果它是object类型的话会更灵活。