【发布时间】:2014-08-18 05:31:35
【问题描述】:
我创建了一个用户控件。它有一个按钮和一个文本块。我想使用 Dependency 属性将图像传递给该按钮。但是当我尝试在其他页面中调用该用户控件时,它会显示一些错误。这是我的代码..
用户 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
我在其他页面中调用此用户控件。
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 上传递图像。我该怎么做?
【问题讨论】:
-
AppBarContent的类型为Image,您将其设置为字符串。 -
#region AppBarContent 属性 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
-
不。我设置为仅图像.. 我没有明白你的意思
-
如果你想传递一个文件名,也许你可以考虑
ImageSource而不是Image。 ImageSource 具有来自字符串的转换器。或者你可以编写一个转换器将图像路径字符串转换为图像。 -
@JohnPaul - 文件路径和图像是完全不同的东西。您需要传递使用文件路径创建的 Image 对象。将 DP 更改为类型字符串或从 UI 绑定 Image 对象。
标签: c# wpf xaml user-controls