我知道這個問題可能聽起來類似於我的最後一個,但這是從別人的建議採取不同的方法。走一條不同的路線,我試圖將listview中的whats綁定到幾個文本框。我對WPF非常陌生,不知道我是否做得對。我想這是一個MVVP方法。目前該計劃沒有多大作用。在列表視圖中有一個項目,但它是空的(沒有名字,年齡,等級)。當我嘗試在列表視圖上選擇它時,我可以看到它突出顯示的項目b/c。任何幫助是極大的讚賞。使用ObservableCollection和綁定WPF
有一個類可以創建一個字符串名稱,int age,int grade並且具有getter和setter方法。類的名稱叫做坎珀(年齡,年級,姓名)
類BindingCamper:做的ObservableCollection
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>();
}
另一類(頁):
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來綁定一切。 :
<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" />
請給我們看看'露營'類 – SLaks
好吧,我編輯它。 :) – TMan
如果您不熟悉數據綁定,請閱讀[概述](http://msdn.microsoft.com/zh-cn/library/ms752347.aspx)... –