Silverlight 中的动态(编程)ItemTemplate(DataTemplate)?
我有这样的代码:
<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>
How can I create a DataTemplate/ItemTemplate in the code Behind with Silverlight?
I have this code:
<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>
How can I create a DataTemplate/ItemTemplate in the code behind with Silverlight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要动态创建
DataTemplate
,您需要使用XDocument
或XmlTextWriter
等 XML 操作对象构建描述它的 Xaml 字符串。此 Xaml 的根必须是 DataTemplate 本身(请记住包含正确的命名空间)。然后,您可以传递生成的字符串
XamlReader.Load
,该字符串将创建DataTemplate
,然后将其分配给ListBox 的
。ItemTemplate
属性To create a
DataTemplate
dynamically you need to build the Xaml string that describes it using XML manipulation objects such asXDocument
orXmlTextWriter
. The root of this Xaml needs to be theDataTemplate
itself (remember to include or the correct namespaces).You can then pass the resulting string
XamlReader.Load
which will create theDataTemplate
which you then assign to theItemTemplate
property of theListBox
.