【问题标题】:Dynamic (programmaticaly) ItemTemplate (DataTemplate) in Silverlight?Silverlight中的动态(编程)ItemTemplate(DataTemplate)?
【发布时间】:2010-03-01 11:06:35
【问题描述】:

我有这个代码:

      <Grid x:Name="LayoutRoot">
   <Grid HorizontalAlignment="Left" Height="900" Width="1200">
   <Grid.RowDefinitions>
            <RowDefinition Height="300"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="300"></ColumnDefinition>
        </Grid.ColumnDefinitions>
    <ListBox x:Name="lst1" Width="300" Height="100" Grid.Row="0" Grid.Column="0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" Margin="3">
                            <TextBlock Text="Id:" Foreground="Brown"></TextBlock>
                            <TextBlock Text="{Binding Id}" Foreground="Blue"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
  </ListBox>
  <ListBox x:Name="lst1" Width="300" Height="100" Grid.Row="0" Grid.Column="1"/>
  <ListBox x:Name="lst2" Width="300" Height="100" Grid.Row="0" Grid.Column="2"/>
  <ListBox x:Name="lst3" Width="300" Height="100" Grid.Row="0" Grid.Column="3"/>
  </Grid>
    </Grid>

如何使用 Silverlight 在后面的代码中创建 DataTemplate/ItemTemplate?

【问题讨论】:

    标签: c# silverlight silverlight-3.0 datatemplate itemtemplate


    【解决方案1】:

    要动态创建DataTemplate,您需要使用诸如XDocumentXmlTextWriter 等XML 操作对象来构建描述它的Xaml 字符串。此 Xaml 的根需要是 DataTemplate 本身(请记住包含或正确的命名空间)。

    然后您可以传递结果字符串XamlReader.Load,这将创建DataTemplate,然后您将其分配给ListBoxItemTemplate 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-10
      • 2012-12-20
      • 1970-01-01
      • 2011-02-01
      • 2020-05-28
      • 2011-01-20
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多