在MODEL中,有一个ToArray
。
在VIEWMODEL中,有trait T
class A (name: String) extends T
case class B (cls: A)
object Main {
def main(args: Array[String]) {
val a: A = new A("John")
val b: B = new B(a)
checkType(b)
}
def checkType(cls: AnyRef) {
cls match {
case B(input) => println("your name is " + input.name)
case _ => println("others")
}
}
}
。
我想在VIEW的ComboBox进行绑定
但是,当我将class A (n: String) extends T
{
var name: String = n
}
绑定到MODEL列表时,VIEW会转到VIEWMODEL。
因此绑定始终失败。 如何解决这个问题?
list<string>
查看
IList<MODEL>
答案 0 :(得分:0)
我猜您有一个数据网格,并且您想用来自ComboItems的字符串通过组合框填充一列。如果这是您的目标,则必须牢记以下几点。
根据我所说的可以使用传统WPF进行的操作,您的代码似乎是UWP
MainWindow.xaml:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx /2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<Grid>
<DataGrid ItemsSource="{Binding ModelList}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" />
<DataGridTemplateColumn Header="MyComboColumn" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource ="{Binding ComboItems}" BorderThickness="0" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ViewModel vm = new ViewModel();
DataContext = vm;
vm.ModelList = new BindingList<Model>();
Model md = new Model();
md.ComboItems = new List<string>();
md.ComboItems.Add("string1");
md.ComboItems.Add("string2");
md.ComboItems.Add("string3");
vm.ModelList.Add(md);
md = new Model();
md.ComboItems = new List<string>();
md.ComboItems.Add("string4");
md.ComboItems.Add("string5");
md.ComboItems.Add("string6");
vm.ModelList.Add(md);
}
}
}
ViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
namespace WpfApplication2
{
public class ViewModel : INotifyPropertyChanged
{
private BindingList<Model> _modelList;
public BindingList<Model> ModelList
{
get { return _modelList; }
set { _modelList = value;
NotifyPropertyChanged("ModelList");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
}
型号
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WpfApplication2
{
public class Model
{
private IList<string> _comboItems;
public IList<string> ComboItems
{
get { return _comboItems; }
set { _comboItems = value; }
}
}
}