WPF 添加生成内容的最佳方式
WPF Best way to add generated Content
目前是这样做的:
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="2">
<ListBox Name="lb_right" Background="Red">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:Textfeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<TextBox Text="{Binding text}" HorizontalAlignment="Stretch"></TextBox>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Datefeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<DatePicker HorizontalAlignment="Stretch" Text="{Binding zeit,StringFormat='dd.MMM.yyyy'}"></DatePicker>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
</Grid>
填充我使用
ObservableCollection<Object> t = new ObservableCollection<Object>();
并设置为listview的Itemsource,效果很好,但是,它是一个listview,有没有像Wrappanel这样的带有Itemsource的Element?
好的,谢谢,我找到了解决方案:ItemsControl(以前从未 used/saw 它)
<ItemsControl Name="lb_right" Background="Red">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Textfeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<TextBox Text="{Binding text}" HorizontalAlignment="Stretch"></TextBox>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Datefeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<DatePicker HorizontalAlignment="Stretch" Text="{Binding zeit,StringFormat='dd.MMM.yyyy'}"></DatePicker>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>
目前是这样做的:
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Column="2">
<ListBox Name="lb_right" Background="Red">
<ListBox.Resources>
<DataTemplate DataType="{x:Type local:Textfeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<TextBox Text="{Binding text}" HorizontalAlignment="Stretch"></TextBox>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Datefeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<DatePicker HorizontalAlignment="Stretch" Text="{Binding zeit,StringFormat='dd.MMM.yyyy'}"></DatePicker>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
</Grid>
填充我使用
ObservableCollection<Object> t = new ObservableCollection<Object>();
并设置为listview的Itemsource,效果很好,但是,它是一个listview,有没有像Wrappanel这样的带有Itemsource的Element?
好的,谢谢,我找到了解决方案:ItemsControl(以前从未 used/saw 它)
<ItemsControl Name="lb_right" Background="Red">
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:Textfeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<TextBox Text="{Binding text}" HorizontalAlignment="Stretch"></TextBox>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Datefeld_Template}">
<WrapPanel HorizontalAlignment="Stretch">
<Label Content="{Binding name}"></Label>
<DatePicker HorizontalAlignment="Stretch" Text="{Binding zeit,StringFormat='dd.MMM.yyyy'}"></DatePicker>
<Label Foreground="Red" IsEnabled="{Binding Pflicht}">Pflichtfeld!</Label>
</WrapPanel>
</DataTemplate>
</ItemsControl.Resources>
</ItemsControl>