[英]How to Pass Image to a Button in UserControl using Dependency Property
I have created an UserControl. 我已经创建了一个UserControl。 It has a Button and a textblock.
它具有一个按钮和一个文本块。 I want to pass an Image to that Button using Dependency property.
我想使用Dependency属性将图像传递给该按钮。 But when I try to call that usercontrol in some other page, its showing some error.
但是,当我尝试在其他页面中调用该用户控件时,它显示了一些错误。 This is my code..
这是我的代码。
User Control.xaml: 用户Control.xaml:
<UserControl x:Class="....UserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Border Grid.Row="0"
Name="borMain"
Style="{StaticResource ButtonImageTextBorderStyle}"
MouseEnter="borMain_MouseEnter"
MouseLeave="borMain_MouseLeave"
PreviewMouseLeftButtonDown="borMain_MouseLeftButtonDown" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="MouseStates">
<VisualState Name="MouseEnter">
<Storyboard>
<ColorAnimation To="Black"
Duration="0:0:00.1"
Storyboard.TargetName="borMain"
Storyboard.TargetProperty="BorderBrush.Color" />
<ColorAnimation To="Black"
Duration="0:0:00.1"
Storyboard.TargetName="borMain"
Storyboard.TargetProperty="Background.Color" />
<ThicknessAnimation To="4,1,4,4"
Duration="0:0:00.1"
Storyboard.TargetName="borMain"
Storyboard.TargetProperty="Margin" />
</Storyboard>
</VisualState>
<VisualState Name="MouseLeave" />
<VisualStateGroup.Transitions>
<VisualTransition To="MouseLeave" />
<VisualTransition To="MouseEnter" />
</VisualStateGroup.Transitions>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Button Content="{Binding Path=AppBarContent}"
Style="{StaticResource ButtonImageTextImageStyle}" />
</Border>
<TextBlock Grid.Row="1"
Name="tbText"
Style="{StaticResource ButtonImageTextTextBlockStyle}"
Text="{Binding Path=Text}" />
</Grid>
</UserControl>
UserControl.xaml.cs: UserControl.xaml.cs:
[DefaultEvent("Click")]
public partial class SystemUnitUserControl : UserControl
{
public UserControl()
{
InitializeComponent();
this.DataContext = this;
}
#region Text Property
public string Text
{
get { return (string)GetValue(TextProperty); }
set
{
SetValue(TextProperty, value);
if (string.IsNullOrEmpty(value))
tbText.Visibility = Visibility.Collapsed;
}
}
#region AppBarContent Property
public Image AppBarContent
{
get { return (Image)GetValue(AppBarContentProperty); }
set { SetValue(AppBarContentProperty, value); }
}
public static readonly DependencyProperty AppBarContentProperty =
DependencyProperty.Register("AppBarContent", typeof(Image), typeof(SystemUnitUserControl), null);
#endregion
#region MouseLeftDown Event
private void borMain_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
RaiseClick(e);
}
#endregion
#region Click Event Procedure
public delegate void ClickEventHandler(object sender, RoutedEventArgs e);
public event ClickEventHandler Click;
protected void RaiseClick(RoutedEventArgs e)
{
if (null != Click)
Click(this, e);
}
#endregion
#region Visual State Animations
private void borMain_MouseEnter(object sender, MouseEventArgs e)
{
VisualStateManager.GoToElementState(borMain, "MouseEnter", true);
}
private void borMain_MouseLeave(object sender, MouseEventArgs e)
{
VisualStateManager.GoToElementState(borMain, "MouseLeave", true);
}
#endregion
I am calling this Usercontrol in other page. 我在其他页面中将此用户控件称为。
Window1.xaml: Window1.xaml:
...
xmlns:my="clr-namespace:....UserControls"
...
<my:UserControl x:Name="actionRectDuct"
AppBarContent="F:\..\..\Assets\offline.jpg"
Text="Button 1" />
Its not going into that page. 它不会进入该页面。 Its showing the following error..
其显示以下错误。
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll PresentationFramework.dll中发生类型为'System.Windows.Markup.XamlParseException'的第一次机会异常
Additional information: 'Set property 'UnitUserControl.AppBarContent' threw an exception.' 附加信息:“设置属性'UnitUserControl.AppBarContent'引发异常。” Line number '38' and line position '41'.
行号“ 38”和行位置“ 41”。
I want to pass image on the AppBarContent.. how can i do this?? 我想在AppBarContent上传递图像。我该怎么做?
here is how you can do the same with your code 这是您如何对代码进行相同的操作
<my:UserControl x:Name="actionRectDuct"
Text="Button 1" >
<my:UserControl.AppBarContent>
<Image Source="F:\..\..\Assets\offline.jpg" />
</my:UserControl.AppBarContent>
</my:UserControl>
instead of passing an string to the AppBarContent
property which accepts an Image, we'll now pass the an instance of Image having the source as the desired file. 现在,我们将传递具有源作为所需文件的Image实例,而不是将字符串传递给接受Image的
AppBarContent
属性。 so as long as the source is correct you'll be able to get the desired image in SystemUnitUserControl 因此只要来源正确,您就可以在SystemUnitUserControl中获得所需的图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.