【问题标题】:Silverlight Binding To A User Control Within A DataTemplateSilverlight 绑定到 DataTemplate 中的用户控件
【发布时间】:2011-11-22 15:49:09
【问题描述】:

在 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");
  }
}

【问题讨论】:

  • 你能提供更多细节(Xaml)吗?它在答案中提供了相关示例。谢谢。
  • 按要求完成。感谢您的关注。

标签: silverlight data-binding binding mvvm datatemplate


【解决方案1】:

不知道您为什么使用 ItemsControl 来显示内容,也许您可​​以尝试使用 ContentControl。

<ContentControl Content="{Binding Contents}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" ...

注意 Horizo​​ntalContentAlignment 和 VerticalContentAlignment 属性,这些属性设置控件内容的对齐方式,因此如果将它们设置为“拉伸”,那么内容应该适合所有可用空间。

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2013-09-20
    • 2015-01-29
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2016-07-05
    • 2021-08-10
    • 2011-10-13
    相关资源
    最近更新 更多