简体   繁体   English

将多个滚动数据表放入堆栈面板 WPF 上的网格中

[英]Place multiple scrolling data tables into a grid on a stack panel WPF

I am new to WPF and XAML coming from VB.NET and WinForms so I apologize if this post is off.我是来自 VB.NET 和 WinForms 的 WPF 和 XAML 的新手,所以如果这篇文章被关闭,我深表歉意。 I am trying to set up multiple sql binded data tables into a stack panel that has a grid in it.我正在尝试将多个 sql 绑定数据表设置到一个包含网格的堆栈面板中。 The stack panel has a grid which I was then hoping to place multiple data tables in so that it would be a "row" of data tables.堆栈面板有一个网格,然后我希望在其中放置多个数据表,以便它成为数据表的“行”。 I have ran into a problem where each data table has more entries than the data table has space for and there is no scroll bar.我遇到了一个问题,其中每个数据表的条目多于数据表的空间,并且没有滚动条。 I tried using a scroll viewer but was unsuccessful and all the posts I have found so far have not been able to help me due to the fact they will force me to re-create my xaml tree.我尝试使用滚动查看器但没有成功,到目前为止我发现的所有帖子都无法帮助我,因为它们会迫使我重新创建我的 xaml 树。 Here is my code so far.到目前为止,这是我的代码。

------XAML------- ------XAML-------

 <Window x:Class="Lines.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="251.7" Width="1058">

<Grid x:Name="GridMain">
    <StackPanel x:Name="StackMain" HorizontalAlignment="Left" Height="100" Margin="10,10,0,0" VerticalAlignment="Top" Width="1030" >
        <TextBlock FontSize="18" Foreground="Black" FontWeight="Bold" Text="Line 2" HorizontalAlignment="Center" />
        <Grid x:Name="Line2Information">
            <ScrollViewer x:Name="SVLine2HotParts" HorizontalAlignment="Left">
                <DataGrid x:Name="DGLine2HotParts">
                </DataGrid>
            </ScrollViewer>
        </Grid>
    </StackPanel>
</Grid>

---------------------------C#---------------- ---------------------------C#----------------

             //SQL command fill 
        SqlConnection conn = new SqlConnection(Connectionstring);
        SqlDataAdapter da = new SqlDataAdapter("SELECT * from Table", conn);
        DataTable ds = new DataTable();
        da.Fill(ds);
        Datagrid.ItemsSource = ds.DefaultView;

So in the XML you can see I have a textblock with a location and then on the next "Row" down I will be setting multiple data tables, hopefully along a grid.因此,在 XML 中,您可以看到我有一个带有位置的文本块,然后在下一个“行”向下我将设置多个数据表,希望沿着网格。 Any help?有什么帮助吗?

You're asking a very broad question here.你在这里问了一个非常广泛的问题。 I think you should do some research into MVVM.我认为您应该对 MVVM 进行一些研究。 It helps to structure a WPF design.它有助于构建 WPF 设计。 The general idea is that you bind an element in the XAML (in the View) to a public property in the ViewModel.一般的想法是将 XAML(在视图中)中的元素绑定到 ViewModel 中的公共属性。 Then changes to the property result in changes to the element and hence the UI.然后对属性的更改会导致对元素和 UI 的更改。 The Model contains the data and will concerned with getting data from the DB.模型包含数据并将关注从数据库获取数据。 You'll likely have a property of type ObservableCollection in the ViewModel bound to the Datagrid.您可能会在绑定到 Datagrid 的 ViewModel 中拥有一个 ObservableCollection 类型的属性。 The ViewModel would populate the ObservableCollection from the Model. ViewModel 将从模型填充 ObservableCollection。 With MVVM you have little code behind.使用 MVVM,您背后的代码很少。 One comment on your "SQL command fill", I'm assuming this is in your code behind.关于您的“SQL 命令填充”的一条评论,我假设这是在您的代码后面。 You need to reference DGLine2HotParts.Datacontext or DGLine2HotParts.ItemSource to add data to the grid.您需要引用 DGLine2HotParts.Datacontext 或 DGLine2HotParts.ItemSource 将数据添加到网格。 This is an attempt to point you in a particular direction rather than a comprehensive answer.这是试图为您指明一个特定的方向,而不是一个全面的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM