简体   繁体   English

WPF绑定不适用于类

[英]WPF Binding doesn't work with class

I have binding in XAML 我在XAML中具有绑定

<ComboBox Name="mark" SelectedIndex="1" ItemsSource="{Binding Path=marks}" HorizontalAlignment="Left" Margin="350,300,0,0" VerticalAlignment="Top" Width="120"/>

In C# code i write 我用C#代码编写

 DataContext = new ViewModel();

Where ViewModel is class 其中ViewModel是类

 public class ViewModel
{
    ......
    public int[] marks = new int[4] { 2, 3, 4, 5 };

Why I have 为什么我有

"System.Windows.Data Error: 40 : BindingExpression path error: 'marks' property not found on 'object' ''ViewModel' (HashCode=46545237)'. BindingExpression:Path=marks; DataItem='ViewModel' (HashCode=46545237); target element is 'ComboBox' (Name='mark'); target property is 'ItemsSource' (type 'IEnumerable')"

marks must be property to be bind-able. marks必须是可绑定的属性。

public class ViewModel
{
    public ViewModel()
    {
        marks = new int[4] { 2, 3, 4, 5 };
    }
    ......
    public int[] marks{get; set;}
}

Some steps are below. 下面是一些步骤。 1. Make a public class like as ViewModel. 1.制作一个公共类,例如ViewModel。 2. And create a public properties like as public int[] marks and also use get and set. 2.创建一个公共属性,如public int []标记,并使用get和set。 3. Then you can create constructor to pass array values. 3.然后,您可以创建构造函数以传递数组值。 4. After this you can binding with combobox using name. 4.之后,您可以使用名称与组合框绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM