Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态创建按钮网格并绑定其文本值?_C#_Wpf_Mvvm - Fatal编程技术网

C# 动态创建按钮网格并绑定其文本值?

C# 动态创建按钮网格并绑定其文本值?,c#,wpf,mvvm,C#,Wpf,Mvvm,实际上我有两个问题 我正在WPF中从事一个电影项目,该项目连接到asp.net Web服务。在一个视图中,我必须显示一场演出的所有预订,为此,我需要在视图中显示类似网格的内容,以显示一个房间中的所有座位,以显示哪个座位是免费/预订/出售的 首先,网格在菜单中只有一个按钮,它绑定到命令LoadReservationCommand以加载所有数据。在此之前,viewmodel不知道网格的大小,因此应该动态创建它。可能吗 例如,一旦我有了grid buttongrid,我就应该将它的文本值绑定到一个数组

实际上我有两个问题

我正在WPF中从事一个电影项目,该项目连接到asp.net Web服务。在一个视图中,我必须显示一场演出的所有预订,为此,我需要在视图中显示类似网格的内容,以显示一个房间中的所有座位,以显示哪个座位是免费/预订/出售的

首先,网格在菜单中只有一个按钮,它绑定到命令LoadReservationCommand以加载所有数据。在此之前,viewmodel不知道网格的大小,因此应该动态创建它。可能吗

例如,一旦我有了grid buttongrid,我就应该将它的文本值绑定到一个数组,该数组由来自viewmodel的免费、保留、出售字符串组成。有没有办法用索引来绑定它

下面是我的ViewModel构造函数的第一部分

谢谢你的帮助

public ReservationViewModel(IMoziAdminModel model, PerformanceDTO performance)
    {
        _model = model;
        _performance = performance;

        LoadReservationCommand = new DelegateCommand(async (param) =>
        {
            //We need to find in which room the performance will be
            try
            {
                await _model.RoomLoadAsync();
                Rooms = new ObservableCollection<RoomDTO>(_model.Rooms);
            }
            catch
            {
                OnMessageApplication("Error (Loading)");
            }

            foreach (var r in Rooms)
            {
                if (r.Room_Id == _performance.Room_Id)
                {
                    row = r.Rows;
                    column= r.Columns;
                }
            } //And now we know how big the room is, and we could create the buttongrid.

            ......

        });

        .......

    }

您只需将这些按钮添加到viewmodel上的集合,然后将其绑定到,该集合将根据您指定的模板生成按钮。

字符串数组?拜托!更糟糕的是

enum SeatState {
    Free,
    // ...
}

class Seat {
    ICommand Buy { get; private set; }
    SeatState State { get; private set; }
    // ...
}

class Room {
    int Rows { get; private set; }
    int Colums { get; private set; }
    IEnumerable<Seat> Seats { get; private set; }
    // ...
}

class ViewModel : BaseClassImplementingINotifyPropertyChange {
    Room CurrentRoom {
        get { ... }
        private set { ... } // Raise event
    }
    ICommand LoadCurrentRoom { get; private set; }
}
然后是景色

<...>
    <ItemsControl ItemsSource="{Binding CurrentRoom.Seats}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid
                    Columns="{Binding CurrentRoom.Columns}"
                    Rows="{Binding CurrentRoom.Rows}"
                />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding Buy}">
                    <TextBlock Text="{Binding State}"/>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</...>

谢谢,我要试试这个!你的链接消失了,你答案的价值也消失了: