WPF - 将用户控件添加到列表框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19130943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:46:11  来源:igfitidea点击:

WPF - adding user control to listbox

wpfuser-controls

提问by Saket

I am trying to add a usercontrolto a ListBox.

我正在尝试将用户控件添加到ListBox

My usercontrol contains a collection of basic elements like textbox and dropdowns, in a fashion so that it creates a row of elements.

我的用户控件包含一组基本元素,如文本框和下拉列表,以某种方式创建一行元素。

The code for ListBox in my main window is as -

我的主窗口中 ListBox 的代码是 -

<GroupBox FontWeight="SemiBold" Foreground="#FF0CAEF9" Name="gbAddProducts" Style="{x:Null}" Header="ADD PRODUCTS" HorizontalAlignment="Left" Margin="0,256,0,0" VerticalAlignment="Top" Width="990" Height="207">
        <ListBox Name="lstboxAddProduct" ItemsSource="{Binding Path=AddNewProductRowViewModelList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" BorderThickness="0" Margin="0,10,-2,23">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <ContentControl Content="{Binding AddNewProductRowViewModel}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
                <Validation.ErrorTemplate>
                    <ControlTemplate>
                        <StackPanel>
                            <AdornedElementPlaceholder x:Name="aepForError"/>
                            <ItemsControl ItemsSource="{Binding}">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding ErrorContent}"  Foreground="White" Background="#DC000C" />
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </StackPanel>
                    </ControlTemplate>
                </Validation.ErrorTemplate>
            </ListBox>

        </GroupBox>

Here AddNewProductRowViewModelListis my user control list containing 5 controls. The Problem that I see is that when I run the code, the screen has 5 rows, ie I can click on the area to figure out theer are 5 rows as that section gets highlighted. But they are not visible.

这里AddNewProductRowViewModelList是我的用户控件列表,包含 5 个控件。我看到的问题是,当我运行代码时,屏幕有 5 行,即我可以单击该区域以找出 5 行,因为该部分被突出显示。但它们是不可见的。

Could it be some 'bring to front' sort of issue.

可能是某种“带到前台”的问题。

Please advise.

请指教。

Thanks in advance.

提前致谢。

回答by Daniel

I would do it this way instead:

我会这样做:

<ListBox.ItemTemplate>
   <DataTemplate>
      <StackPanel Orientation="Vertical">
         <ContentControl>
            <local:AddNewProductRowView Datacontext="{Binding AddNewProductRowViewModel}"/>
          </ContentControl>
         </StackPanel>
       </DataTemplate>
     </ListBox.ItemTemplate>