【问题标题】:WPF Best way to add generated ContentWPF 添加生成内容的最佳方式
【发布时间】:2019-08-05 12:53:07
【问题描述】:

目前这样做:

<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的元素?

【问题讨论】:

    标签: c# wpf wrappanel itemsource


    【解决方案1】:

    好的,谢谢,我找到了解决方案:ItemsControl(以前从未使用过/见过)

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多