在Silverlight中自定了一个对象,并在对象中定义了一个UserControl属性,然后在页面资源中生成一个该对象的实例,不用后代代码,能否直接在XAML中,直接将XAML页面的UserControl对象的实例赋值给自定对象的属性呢? 如下是代码, 有办法吗? 请各位高手,大侠帮帮忙.给个明确答复,在线等,回答正确马上给分.先感谢了
自定义对象
using System.Windows.Controls;
namespace SApp
{
public class CustomizeObject
{
public UserControl Control { get; set; }
}
}
XAML页
<UserControl xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" x:Class="SApp.MainPage"
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"
xmlns:S="clr-namespace:SApp"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<UserControl.Resources>
<S:CustomizeObject x:Key="item" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
</Grid>
</UserControl>
http://www.cnblogs.com/prism/archive/2010/07/23/1783408.html
这个视频后面有的讲。
public UserControl Control
{
get
{
return (UserControl)GetValue(ControlProperty);
}
set
{
SetValue(ControlProperty, value);
}
}
// Using a DependencyProperty as the backing store for Control. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ControlProperty =
DependencyProperty.Register("Control", typeof(UserControl), typeof(UserControl), new PropertyMetadata(null));
用Element to Element Binding试一下.
可以:如下面的代码:
XAML :
<UserControl.Resources>
<s:CustomizeObject x:Key="item" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White">
<s:CustomizeObject x:Name="a" Control="{StaticResource item}" ></s:CustomizeObject>
</Grid>
CS :
public class CustomizeObject :UserControl
{
public UserControl Control
{ get; set; }
}
我测试过, 可以用的,你试下看、希望对你有帮助。