如何在包含一些相同类型的自定义用户控件的堆栈面板的 WPF 列表框中创建?列表框和堆栈面板应该是可滚动的。ListBox.Items 是动态添加的,UserControls 也是动态添加到 StackPanels 的。我试过谷歌,没有找到类似的东西,只有带有复选框和图像的简单列表框示例。
1 回答
            0        
        
		
使用 ScrollViewer 和 ItemsControl
例如:
<ScrollViewer HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Visible">
 <ItemsControl x:Name="personsColl" ItemsSource="{Binding Path=Persons,
                                              ElementName=CustomPersonInfoColl,
                                              Mode=TwoWay,
                                              UpdateSourceTrigger=PropertyChanged}">
 <ItemsControl.ItemTemplate>
       <DataTemplate>
         <Grid x:Name="personMainContainer">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"  />
    </Grid.ColumnDefinitions>
        <!-- context for reapeat -->
        <local:PersonInfo ....  />
    </Grid> 
</DataTemplate>
 </ItemsControl.ItemTemplate>
 <ItemsControl.ItemsPanel>
             <ItemsPanelTemplate>
                     <VirtualizingStackPanel Orientation="Vertical"/>
              </ItemsPanelTemplate>
 </ItemsControl.ItemsPanel>
</ItemsControl>
不要忘记
<UserControl  x:Name="CustomPersonInfoColl" .... >
并返回代码
public partial class PersonInfoCollection : UserControl, INotifyPropertyChanged
{
    [Bindable(true)]
    public IList<ShortPersonInfo> Persons
    {
        get { return (IList<ShortPersonInfo>)this.GetValue(PersonsProperty); }
        set { this.SetValue(PersonsProperty, value); OnPropertyChanged("Persons");}
    }
    public static readonly DependencyProperty PersonsProperty =
        DependencyProperty.Register("Persons",
            typeof(IList<ShortPersonInfo>),
            typeof(PersonInfoCollection));
}
您可以从其他赢取表格或控件中填写人员集合。
于 2013-06-26T11:17:00.383   回答