简体   繁体   English

使用ObservableCollection和绑定WPF

[英]Using ObservableCollection and Binding WPF

I know this question may sound similar to my last one but this is a different approach taking from others advice. 我知道这个问题听起来可能与我的上一个问题类似,但这是一种不同于其他人的建议。 Going a different route I'm trying to bind whats in a listview to a couple of textboxes. 走另一条路线我试图将列表视图中的什么绑定到几个文本框。 I am extremely new to WPF's and Not sure if I'm doing this right. 我对WPF非常陌生,不确定我是否正确行事。 I guess this a MVVP approach. 我想这是一个MVVP方法。 Right now the program doesn't do much. 目前该计划并没有做太多。 There is one item in the listview but it is empty(No name, age, grade). 列表视图中有一个项目,但它是空的(没有名称,年龄,等级)。 I can see the item b/c it highlights when I try to select it on the listview. 当我尝试在列表视图中选择它时,我可以看到它突出显示的项目b / c。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

There is class that makes a string name, int age ,int grade and has getter and setter methods. 有一个类可以生成字符串名称,int age,int grade,并且具有getter和setter方法。 Name of class is called Camper(age, grade, name) 班级名称叫Camper(年龄,年级,姓名)

Class BindingCamper: making observablecollection 类BindingCamper:进行可观察收集

     public class Camper
 {
public Camper[] requests;
public int[] relationValues;
public String name;
private String school;
public int age;
public int grade;
private Boolean isGrouped = false;
private string group;


      public Camper(int a, int g, String n)
{
    requests = new Camper[4];
    relationValues = new int[4];
    name = n;
    this.age = a;
    this.grade = g;
}

public Camper(String n)
{
    this.requests = new Camper[4];
    this.relationValues = new int[4];
    this.name = n;
}

public Camper()
{

}

// Getter Methods
public string getName()
{
    return name.ToString();
}
public string getSchool()
{
    return this.school;
}
public int getAge()
{
    return this.age;
}
public int getGrade()
{
    return this.grade;
}
public int getRelationValue(int i)
{
    if (i < relationValues.Length)
    {
        return relationValues[i];
    }
    else
    {
        return 0;
    }

}

class BindingCamper
{
    public ObservableCollection<Camper> Campers { get; private set; }

    public BindingCamper()
    {
        Campers = new ObservableCollection<Camper>();

    }

Another class(page) : 另一课(页面):

public partial class CampersPage : Page
{
    MainWindow _parentForm;

    public ObservableCollection<Camper> Campers { get; private set; }

    public CampersPage(MainWindow parent)
    {
        _parentForm = parent;
        InitializeComponent();

       // Campers = new ObservableCollection<Camper>();
        var bindMe = new BindingCamper();
        Camper CampMe = new Camper(3, 4, "Tony Lagarrigue");
        // CampMe.getName();
        bindMe.Campers.Add(CampMe);
        DataContext = bindMe;

XAML to bind everything. XAML绑定一切。 :

      <ListView HorizontalAlignment="Left" Margin="10,10,0,40" x:Name ="listViewCampers" Width="200" SelectionChanged="listViewCampers_SelectionChanged" ItemsSource="{Binding Campers}" DisplayMemberPath="name" IsSynchronizedWithCurrentItem="{x:Null}">

            <ListView.View>
                <GridView>



                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="100"  />
                    <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="40" />
                    <GridViewColumn Header="Grade" DisplayMemberBinding="{Binding Grade}" Width="40" />

                </GridView>
            </ListView.View>
        </ListView>
        <Grid Height="Auto" HorizontalAlignment="Stretch" Margin="209,12,0,0" Name="infoGrid" VerticalAlignment="Stretch">
            <Grid.RowDefinitions>
                <RowDefinition Height="134*" />
                <RowDefinition Height="154*" />
            </Grid.RowDefinitions>
            <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="23,24,0,0" Name="lblName" VerticalAlignment="Top" />
        <TextBox Text="{Binding name, ElementName=listViewCampers}" Height="23" HorizontalAlignment="Left" Margin="23,46,0,0" Name="txtName" VerticalAlignment="Top" Width="120" AcceptsReturn="True" />
            <TextBox Height="23" HorizontalAlignment="Left" Margin="23,103,0,0" Name="txtAge" VerticalAlignment="Top" Width="120" />

You need to create public properties in your viewmodel to bind to. 您需要在viewmodel中创建要绑定的公共属性。

You should never make getSomething() methods in .Net. 你永远不应该在.Net中创建getSomething()方法。

For this code in your ListView declaration: 对于ListView声明中的此代码:

DisplayMemberPath="name"

you are using the field name instead of the property. 您使用的是字段名称而不是属性。 I don't think that databinding works against fields, even if they are public (to be honest, I have never understood why this is the case). 我不认为数据绑定对字段起作用,即使它们是公开的(说实话,我也从未理解为什么会这样)。

It looks like you are doing this in a few other spots, too--make sure you are binding to the property, and make sure the name is correct (it is case sensitve). 看起来你也在其他几个地方这样做 - 确保你绑定到属性,并确保名称是正确的(这是大小写敏感)。

Edit : as SLaks noted (I didn't notice this until he pointed it out), you are using methods as properties. 编辑 :正如SLaks所指出的那样(直到他指出它我才注意到这一点),你正在使用方法作为属性。 I think they need to be actual .Net properties with getters, eg: 我认为他们需要成为具有getter的实际.Net属性,例如:

public string Name
{
  get { return _name; }
}

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

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