我已将用户控件添加到App.xml
<Grid>
<Button Style="{StaticResource PlayerButtonsStyle}"
Width="{Binding ElementName=UC, Path=ImageWidth}"
Height="{Binding ElementName=UC, Path=ImageHeight}">
<Button.Background>
<ImageBrush ImageSource="{Binding ElementName=UC, Path=Image}" />
</Button.Background>
</Button>
</Grid>
在我的主窗口中,添加以下内容以添加按钮。这不会显示我添加为资源的图像。
<local:ImageButton HorizontalAlignment="Left"
Height="25"
Margin="474,430,0,0"
VerticalAlignment="Top"
Width="25"
Image="images/play.png">
</local:ImageButton>
但是,如果我遵循该图像,将显示该图像,但这违背了将其作为用户控件的目的。
<local:ImageButton HorizontalAlignment="Left"
Height="25"
Margin="474,430,0,0"
VerticalAlignment="Top"
Width="25"
Image="images/forward.png">
<local:ImageButton.Background>
<ImageBrush ImageSource="images/play.png"/>
</local:ImageButton.Background>
</local:ImageButton>
这是用户控件
public partial class ImageButton : UserControl
{
public ImageButton()
{
InitializeComponent();
}
public ImageSource Image
{
get { return (ImageSource)GetValue(ImageProperty); }
set { SetValue(ImageProperty, value); }
}
// Using a DependencyProperty as the backing store for Image. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageProperty =
DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null));
public double ImageWidth
{
get { return (double)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageWidth. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageWidthProperty =
DependencyProperty.Register("ImageWidth", typeof(double), typeof(ImageButton), new UIPropertyMetadata(16d));
public double ImageHeight
{
get { return (double)GetValue(ImageHeightProperty); }
set { SetValue(ImageHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for ImageHeight. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ImageHeightProperty =
DependencyProperty.Register("ImageHeight", typeof(double), typeof(ImageButton), new UIPropertyMetadata(16d));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(ImageButton), new UIPropertyMetadata(""));
}
样式定义
<Style x:Key="PlayerButtonsStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<!--<Setter Property="Margin" Value="5"/>-->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="1" BorderBrush="DarkGray" CornerRadius="3" Background="{TemplateBinding Background}" >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="Black" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="CornerRadius" Value="3" />
</Trigger>
<Trigger Property="Button.IsPressed" Value="True" >
<Setter TargetName="border" Property="BorderBrush" Value="Transparent" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想念什么?