Silverlight 绑定到 DataTemplate 内的用户控件
在 Silverlight 中,我有一个 DataTemplate,它绑定到一个对象,该对象包含一个包含 UserControl 的属性。
在 DataTemplate 中,我想绑定到保存 UserControl 的属性,以便 UserControl 显示为 DataTemplate 的一部分。
目前,我正在使用 ItemsControl 并将 ItemsSource 绑定到包含 UserControl 的属性,这是有效的,但是,UserControl 没有填充可用空间,这让我想知道是否有更好的方法来做到这一点。
感谢您的任何帮助。
马丁.
编辑:根据一些 XAML 的要求:
<DataTemplate x:Key="ContentTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBlock Text="Large Content" Grid.Row="0"/>
<ItemsControl ItemsSource="{Binding Contents}" Grid.Row="1" MinHeight="200" MinWidth="300" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
其中,绑定的 Contents 属性如下:
private UserControl _contents;
public UserControl Contents
{
get {return _contents;}
set
{
_contents = value;
NotifyPropertyChanged("Contents");
}
}
In Silverlight, I have a DataTemplate which is bound to an object which contains a property which holds a UserControl.
Within the DataTemplate, I want to bind to the property which holds the UserControl so that the UserControl is displayed as part of the DataTemplate.
At the moment, I am using an ItemsControl and binding the ItemsSource to the property containing the UserControl and this is working, however, the UserControl is not filling the available space and this is making me wonder whether there is a better way of doing this.
Thanks for any help.
Martyn.
EDIT: As requested some XAML:
<DataTemplate x:Key="ContentTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<TextBlock Text="Large Content" Grid.Row="0"/>
<ItemsControl ItemsSource="{Binding Contents}" Grid.Row="1" MinHeight="200" MinWidth="300" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
</DataTemplate>
Where, the Contents property being bound to is as follows:
private UserControl _contents;
public UserControl Contents
{
get {return _contents;}
set
{
_contents = value;
NotifyPropertyChanged("Contents");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么要使用 ItemsControl 来显示内容,也许您可以尝试使用 ContentControl。
请注意 HorizontalContentAlignment 和 VerticalContentAlignment 属性,这些属性设置控件内容的对齐方式,因此如果将它们设置为“拉伸”,则内容应适合所有可用空间。
Don't know why you're using an ItemsControl to show the content, maybe if you try it with a ContentControl.
Note the the properties HorizontalContentAlignment and VerticalContentAlignment, these properties sets the alignament of the control content, so if you set them to "Stretch" then the content should fit all the available space.