开发者

Silverlight Binding To A User Control Within A DataTemplate

开发者 https://www.devze.com 2023-04-06 11:26 出处:网络
In开发者_如何学JAVA Silverlight, I have a DataTemplate which is bound to an object which contains a property which holds a UserControl.

In开发者_如何学JAVA 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");
  }
}


Don't know why you're using an ItemsControl to show the content, maybe if you try it with a ContentControl.

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号