我创建了一个UserControl。它有一个Button和一个文本块。我想使用Dependency属性将Image传递给该Button。但是当我尝试在其他页面中调用该usercontrol时,它显示出一些错误。这是我的代码..
User 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:
[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
我在其他页面中调用此Usercontrol。
Window1.xaml:
...
xmlns:my="clr-namespace:....UserControls"
...
<my:UserControl x:Name="actionRectDuct"
AppBarContent="F:\..\..\Assets\offline.jpg"
Text="Button 1" />
它不会进入那个页面。它显示以下错误..
PresentationFramework.dll中出现'System.Windows.Markup.XamlParseException'类型的第一次机会异常
其他信息:'设置属性'UnitUserControl.AppBarContent'引发了异常。行号'38'和行位置'41'。
我想在AppBarContent上传递图像..我该怎么做?
答案 0 :(得分:0)
以下是您如何对代码执行相同的操作
<my:UserControl x:Name="actionRectDuct"
Text="Button 1" >
<my:UserControl.AppBarContent>
<Image Source="F:\..\..\Assets\offline.jpg" />
</my:UserControl.AppBarContent>
</my:UserControl>
而不是将字符串传递给接受Image的AppBarContent
属性,我们现在传递一个Image实例,该实例将源代码作为所需文件。所以只要源是正确的,你就可以在SystemUnitUserControl中获得所需的图像