c# - 如何从具有连接表的数据库动态创建 WPF 窗口?

标签 c# wpf

我正在尝试制作一个自定义显示板来帮助管理我们的客户房间。

表 1 名为 Rooms [RoomID, RoomNumber, RoomDescription, ADA, RoomStatus]

表 2 名为 Beds [BedID, BedNumber, RoomID, BedEnabled]

表3命名为Clients [ClientID、BedID、RoomID等客户信息字段]

一个房间可以关联多张床。例如101房间有2张床,那么我们可以分配2个客户到101房间。

我一直在尝试使用 Grid 和 StackPanel 列出所有房间。然后在每个房间列出所有床,然后对于每张床,如果分配了客户,我将使用 TextBlock 来显示客户信息。我收集这不是我可以直接在 xaml 中做的事情,但可能需要用循环在后面做一些代码?

<Window x:Class="BoardDisplay.Window1"        
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BoardDisplay"
        mc:Ignorable="d"
        Title="Window1" Height="450" Width="800" Loaded="Window_Loaded">
    <Window.Resources>
        <local:Room_ManagerDataSet x:Key="room_ManagerDataSet"/>
        <CollectionViewSource x:Key="roomsViewSource" Source="{Binding Rooms, Source={StaticResource room_ManagerDataSet}}"/>
        <CollectionViewSource x:Key="bedsViewSource" Source="{Binding Beds, Source={StaticResource room_ManagerDataSet}}"/>
    </Window.Resources>
    <Grid Margin="10">
        <ItemsControl ItemsSource="{Binding Source={StaticResource roomsViewSource}}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding RoomNumber}" Margin="0,0,5,5">
                        <Grid>
                            <ItemsControl ItemsSource="{Binding Source={StaticResource bedsViewSource}}">
                                <ItemsControl.ItemsPanel>
                                    <ItemsPanelTemplate>
                                        <StackPanel/>
                                    </ItemsPanelTemplate>
                                </ItemsControl.ItemsPanel>
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding BedNumber}"/>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </Grid>
                    </TextBlock>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

最佳答案

如果你进行了正确的绑定(bind),我认为你的情况是双向的,那么你应该像这样使用 updatesourcetrigger

https://stackoverflow.com/a/22253816/914284

Text="{Binding Path=SelectedCollectionDevice.BaudRate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}

此外,您还可以使用包装器

关于c# - 如何从具有连接表的数据库动态创建 WPF 窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55797191/

相关文章:

C# - 来自数据库的 WPF/WP 绑定(bind) - 正确的方法? MVVM

c# - .NET 核心 : How can I set the connection string?

c# - C# DataTable 中 DataColumn 的约束?

WPF MVVM如何在 View 更改后重新居中应用程序窗口?

wpf - OpenGL:更多的顶点,更慢的性能

wpf - 隐藏控件时隐藏验证装饰

c# - 如何分离 View 数据和 ViewModel 数据?

c# - 使用 MaxHeight 和 MaxWidth 约束按比例调整图像大小

c# - 如何进入 dll 方法调用或构造函数,以便它询问源代码?

c# - 我可以简化此返回 View 的代码吗?显得很多余