Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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
WPF:如何设置我的模型和绑定策略?_Wpf_Mvvm_Binding_Combobox_Viewmodel - Fatal编程技术网

WPF:如何设置我的模型和绑定策略?

WPF:如何设置我的模型和绑定策略?,wpf,mvvm,binding,combobox,viewmodel,Wpf,Mvvm,Binding,Combobox,Viewmodel,关于这一形象: 此用户界面的TimeTableViewModel如下所示: public string SchoolclassCodeMonday {get;set;} public string SchoolclassCodeTuesday {get;set;} public string SchoolclassCodeWednesday {get;set;} public string SchoolclassCodeThursday {get;set;} public string Sch

关于这一形象:

此用户界面的TimeTableViewModel如下所示:

public string SchoolclassCodeMonday {get;set;}
public string SchoolclassCodeTuesday {get;set;}
public string SchoolclassCodeWednesday {get;set;}
public string SchoolclassCodeThursday {get;set;}
public string SchoolclassCodeFriday {get;set;}
public string SchoolclassCodeSaturday {get;set;}
public string SchoolclassCodeSunday {get;set;}
当我在文本框中仅将属性显示为字符串时,上述方法就可以工作了。 但是我想要的是将每个combox绑定到一个相互可见的集合 学校类代码和组合框的SelectedItem aka DisplayMember必须以某种方式 映射到上述7个属性之一,如果检索到SelectedItem,则必须提供一行 在组合框中选择的所有7个班级中

或者我真正想要的,换句话说;-)

在组合框中显示SchoolclassCodes列表,将
SelectedItem.SchoolclassCode“WeekdayName”的值设置为所选组合框Item.SchoolclassCode的值

嗯,我有一些想法,但都缺乏一些经验,使它们充分发挥作用

我可以为每个属性添加TimeTableViewModelaObservableCollection 学校班级代码但这对我来说似乎是多余的。当每个单元格都有相同的列表,其中包含相同的项目时,为什么要为一行保留7个列表

欢迎对ViewModels结构和Wpf中的绑定提出任何建议:)

更新:我的学校类代码列表是动态创建的,因此我不可能在XAML中使用静态绑定或硬代码字符串项

更新2:

好的,我试着让它与MVVM一起工作:

我必须更改可观察的收集类代码 可观察到的收集学校类代码作为学校类对象 有一个带字符串的学生类引用,这是不可能的

Schoolclass.cs:

public string SchoolclassCode {get;set;}
...
TimeTableWeekViewModel.cs:

public ObservableCollection<Schoolclass> SchoolclassCodes
    {
        get { return _schoolclassCodes; }
        set
        {
            _schoolclassCodes = value;
            this.RaisePropertyChanged("SchoolclassCodes");
        }
    }
公共可观测集合学校类代码
{
获取{return\u学校类代码;}
设置
{
_学校班级代码=值;
此.RaisePropertyChanged(“学校课程代码”);
}
}
XAML:


由于wpf找不到SchoolclassCodes,绑定现在必须是什么样子?

因为列必须自动生成,所以将此代码放在适当的位置(
加载的
事件?)

        ObservableCollection<String> Collection = GetCollection();

        foreach (DataGridComboBoxColumn column in DataGrid1.Columns.OfType<DataGridComboBoxColumn>())
        {
            column.ItemsSource = Collection;
        }
observeCollection Collection=GetCollection();
foreach(DataGrid1.Columns.OfType()中的DataGridComboxColumn列)
{
column.ItemsSource=集合;
}

当然,必须进行一些修改

如果您正在做任何远程复杂的事情,我建议您研究MVVM(模型-视图-视图-模型)模式。此外,在模型/视图模型中实现
INotifyPropertyChanged
也很有用(例如下面示例中的
MyWeek
类)。当您的
SchoolclassCode
属性发生更改时,它将通知任何其他绑定

以下是一些简单的示例代码,可以帮助您入门:

using System.Collections.ObjectModel;
using System.Linq;

namespace BindingSample
{
    public partial class Window1
    {
        public Window1()
        {
            InitializeComponent();
            SchoolclassCodes = new ObservableCollection<string>(
                Enumerable.Range(1, 10).Select(i => "Code #" + i));
            MyWeeks = new ObservableCollection<MyWeek>(
                Enumerable.Range(1, 5).Select(i => new MyWeek() {SchoolclassCodeMonday = SchoolclassCodes.First()}));
            DataContext = this;
        }

        public ObservableCollection<string> SchoolclassCodes { get; private set; }
        public ObservableCollection<MyWeek> MyWeeks { get; private set; }
    }

    public class MyWeek
    {
        public string SchoolclassCodeMonday { get; set; }
        public string SchoolclassCodeTuesday { get; set; }
        public string SchoolclassCodeWednesday { get; set; }
        public string SchoolclassCodeThursday { get; set; }
        public string SchoolclassCodeFriday { get; set; }
        public string SchoolclassCodeSaturday { get; set; }
        public string SchoolclassCodeSunday { get; set; }
    }
}
使用System.Collections.ObjectModel;
使用System.Linq;
名称空间绑定示例
{
公共部分类窗口1
{
公共窗口1()
{
初始化组件();
SchoolclassCodes=新的ObservableCollection(
范围(1,10)。选择(i=>“Code#”+i);
MyWeeks=新的可观察收集(
范围(1,5)。选择(i=>newmyweek(){SchoolclassCodeMonday=SchoolclassCodes.First()});
DataContext=this;
}
公共可观测集合学校类代码{get;private set;}
公共ObservableCollection MyWeeks{get;private set;}
}
公共课MyWeek
{
公共字符串SchoolclassCodeMonday{get;set;}
公共字符串SchoolclassCodeTuesday{get;set;}
公共字符串SchoolClassCodesTriday{get;set;}
公共字符串SchoolClassCodesThready{get;set;}
公共字符串SchoolclassCodeFriday{get;set;}
公共字符串SchoolclassCodeSaturday{get;set;}
公共字符串SchoolclassCodeSunday{get;set;}
}
}


您不需要乱弄DataGrid,当然也不需要创建任何具有七个不同属性的类,您必须为它们实现属性更改通知。网格控件使日历变得简单

视图模型应该有三个类:
,和
Week
类包含
Day
对象的列表,而
Month
类包含
Week
对象的列表。每个类都应该有对其父类的引用,即
天中的
周和
周中的

Week
类的构造函数应将其
Days
属性初始化为7个
Day
对象的列表。
Month
类的构造函数中必须有更具体的逻辑来设置其
Weeks
属性;我把这个留给你

Day
对象应公开以下属性:

public DayOfWeek DayNumber { get; private set; }
public ObservableCollection<SchoolclassCode> Codes { get { return Week.Codes; } }
然后,您可以定义显示天数的数据模板:

<DataTemplate DataType="{x:Type local:Day}">
   <Grid>
      <Grid.ColumnDefinitions>
         <ColumnDefinition SharedSizeGroup="Sunday"/>
         <ColumnDefinition SharedSizeGroup="Monday"/>
         <ColumnDefinition SharedSizeGroup="Tuesday"/>
         <ColumnDefinition SharedSizeGroup="Wednesday"/>
         <ColumnDefinition SharedSizeGroup="Thursday"/>
         <ColumnDefinition SharedSizeGroup="Friday"/>
         <ColumnDefinition SharedSizeGroup="Saturday"/>
      </Grid.ColumnDefinitions>
      <ComboBox 
         Grid.Column="{Binding DayNumber}" 
         ItemsSource="{Binding Codes}" 
         SelectedValue="{Binding Code, Mode=TwoWay}"/>
   </Grid>
</DataTemplate>

和星期:

<DataTemplate DataType="{x:Type Week}">
  <ItemsControl ItemsSource="{Binding Days}">
    <ItemsControl.ItemTemplate>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

及月份:

<DataTemplate DataType="{x:Type Month}">
  <ItemsControl ItemsSource="{Binding Weeks}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

将日名称和周数添加到上述模板是非常简单的
<DataTemplate DataType="{x:Type Week}">
  <ItemsControl ItemsSource="{Binding Days}">
    <ItemsControl.ItemTemplate>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>
<DataTemplate DataType="{x:Type Month}">
  <ItemsControl ItemsSource="{Binding Weeks}" Grid.IsSharedSizeScope="True">
    <ItemsControl.ItemTemplate>
      <ItemsPanelTemplate>
        <StackPanel Orientation="Vertical"/>
      </ItemsPanelTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>