首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的情况下使用mvvm (ObservableCollection in ObservableCollection)绑定ObservableCollection

如何在我的情况下使用mvvm (ObservableCollection in ObservableCollection)绑定ObservableCollection
EN

Stack Overflow用户
提问于 2015-09-22 11:55:08
回答 3查看 123关注 0票数 1

我所处的情况是“ObservableCollection of”"tablegenerateModel“类进一步包含"column_Data”类的ObservableCollection,而"column_Data“类包含三个必须绑定到包含三列的DataGrid的UI元素。您可以复制粘贴我的代码,如果您愿意的话,我已经为您编写了所有的类和xaml,以验证我的代码,所以我的Model.cs如下所示:

代码语言:javascript
复制
  public class tablegenerateModel {

    public ObservableCollection < column_Data > Column_data_List {
        get;
        set;
    }
    public int NumberOfColumns {
        get;
        set;
    }

    private ICommand _CreatColumnCommand; //THIS IS THE BUTTON EVENT GENERATED WHEN USER PRESS AN INTEGER VALUE IN "NumberOfColumns" AND PRESS "Create" button. So the "Column_data_List" will repeat that many times the value user entered
    ViewModel vm;
    public ICommand CreatColumnCommand {
        get {
            if (_CreatColumnCommand == null) {
                _CreatColumnCommand = new RelayCommand(
                    param => vm = new ViewModel(this)
                );
            }
            return _CreatColumnCommand;
        }
    }
}
public class column_Data {
    public string Column_Name {
        get;
        set;
    }
}

我的视图模型是:

代码语言:javascript
复制
   public class ViewModel: INotifyPropertyChanged {
    private ObservableCollection < tablegenerateModel > _lb_GlobalList;
    private ObservableCollection < column_Data > _Column_data_List;

    public ObservableCollection < tablegenerateModel > lb_GlobalList {
        get {
            return _lb_GlobalList;
        }
        set {
            if (value != _lb_GlobalList) {
                _lb_GlobalList = value;
                RaisePropertyChanged("lb_GlobalList");
            }
        }
    }

    public ObservableCollection < column_Data > Column_data_List {
        get {
            return _Column_data_List;
        }
        set {
            if (value != _Column_data_List) {
                _Column_data_List = value;
                RaisePropertyChanged("Column_data_List");
            }
        }
    }

    public ICommand ClickCommand

    private bool _canExecute;
    public ViewModel() {
        _canExecute = true;
        lb_GlobalList = new ObservableCollection < tablegenerateModel > ();
        for (int i = 1; i < 6; i++) {
            //Add To it data here
        }
    }

    public ViewModel(tablegenerateModel tablegenerateModels) //IT WILL BE CALLED WHEN BUTTON WILL BE CLICKED (see tablegenerateModel class ICommand for it)
        {
            Column_data_List = new ObservableCollection < column_Data > ();
            for (int i = 1; i < tablegenerateModels.NumberOfColumns; i++) { //Add data here
            }
            MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);
        }

}
}

意见是:

代码语言:javascript
复制
 <Window x:Class="PyXgen.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vM="clr-namespace:PyXgen" Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <vM:ViewModel></vM:ViewModel>
    </Window.DataContext>
    <Grid Name="ButtonsContainer">


        <ListBox Grid.Row="1" ItemsSource="{Binding lb_GlobalList}" ScrollViewer.VerticalScrollBarVisibility="Auto" BorderBrush="Gray" Margin="0,30,0,0" Grid.RowSpan="2">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel>
                            <Grid Margin="0,2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="45" />
                                    <RowDefinition Height="45" />
                                    <RowDefinition Height="45" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                    <RowDefinition Height="*" />
                                </Grid.RowDefinitions>

                                <Grid Grid.Row="4">
                                    <StackPanel>
                                        <Grid Margin="0,2">


                                            Grid.Row="0" Width="80px" Height="20px" Margin="20,5,0,0" Name="fname4"/>
                                            <Button Grid.Row="1" Grid.Column="1" Height="25" Width="70" Content="Create" Command="{Binding CreatColumnCommand}" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
                                        </Grid>
                                    </StackPanel>
                                </Grid>
                                <Grid Margin="0,2" Grid.Row="5">
                                    <DataGrid x:Name="gvSelectedCourses" Grid.Column="1" HorizontalAlignment="Center" ItemsSource="{Binding Column_data_List , Mode=TwoWay}" AutoGenerateColumns="False" Width="450">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn Header="Column Name" Binding="{Binding  ,Mode=TwoWay}" Width="150" />
                                        </DataGrid.Columns>
                                    </DataGrid>
                                </Grid>
                            </Grid>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

单击"_CreatColumnCommand“按钮(在tableGenerateModel类中)将调用传递"this”对象的ViewModel构造器,以获取输入的整数值,以重复输入的整数值的DataGrid行数(该整数值被输入到同一类的"NumberOfColumns“文本框中)。

现在的问题是如何绑定"_Column_data_List“,以便它将添加和更新DataGrid的行(通过用户在textbox中输入的整数值)。

当前,当我在文本框中输入整数值并单击"create“按钮时,将使用ViewModel弹出窗口调用MessageBox构造器。

代码语言:javascript
复制
 MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);

如何将此"_Column_data_List“绑定到这样的位置:它将添加或更新DataGrid的行(通过用户在textbox中输入的intger值,在上面的快照中)。

有人能帮我吗?

EN

回答 3

Stack Overflow用户

发布于 2015-09-22 13:15:05

按以下方式更新代码。您已经在tablegeneratedModel中绑定了可观察的集合,但是您正在更新ViewModel上的一个集合。

代码语言:javascript
复制
public ViewModel(tablegenerateModel tablegenerateModels) //IT WILL BE CALLED WHEN BUTTON WILL BE CLICKED (see tablegenerateModel class ICommand for it)
        {
            tablegenerateModels.Column_data_List = new ObservableCollection<column_Data>();
            for (int i = 1; i < tablegenerateModels.NumberOfColumns; i++)
            {
                Column_data_List.Add(lb_col = new column_Data()
                {
                    Column_Name = "ColumnName" + i,
                    Data_Size = i,
                    Data_types = "Double" + i
                });
            }           
            MessageBox.Show("The NumberOfColumns value is:" + tablegenerateModels.NumberOfColumns);
        }
票数 2
EN

Stack Overflow用户

发布于 2015-09-22 12:36:06

“CreatColumnCommand”拼写错误,但一直如此,所以这不是您的问题。但是你的“CreatColumnCommand`”不是在视图模型中,而是在模型中,这是问题所在吗?

票数 0
EN

Stack Overflow用户

发布于 2015-09-23 04:14:17

在给定的情况下,我必须在ObservableCollection中有ObservableCollection,但是如果您看到我的ViewModel,我将尝试添加到ViewModel中的Column_data_List中,而我应该在模型本身中添加它(因为视图不能同时处理lb_GlobalList和Column_data_List)。我做了这件事对我来说很管用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32716226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档