[英]C# WPF - Can't bind list items to gridview columns
I'm trying to load a list with multiple properties into a gridview column in a listview. 我正在尝试将具有多个属性的列表加载到listview的gridview列中。 I did setup a model class, pulled network adapters into a list and also wrote down the binding in the xaml code.
我确实设置了一个模型类,将网络适配器放入列表中,并且还在xaml代码中写下了绑定。 It's still not working.
它仍然无法正常工作。
I probobaly forgot to implement a step or used the wrong logic. 我可能忘记执行某个步骤或使用了错误的逻辑。 Also I tried to look into other threads, but none of them seemed to fix the issue I have.
我也尝试研究其他线程,但是似乎没有一个线程可以解决我遇到的问题。 Thanks for your help.
谢谢你的帮助。
<ListView Grid.Column="0" Grid.ColumnSpan="2" Width="auto" Margin="10" Name="ListView">
<ListView.View>
<GridView AllowsColumnReorder="true" x:Name="GridView">
<GridViewColumn Header="Name" Width="auto" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Interface" Width="auto" DisplayMemberBinding="{Binding Interface}"/>
<GridViewColumn Header="Status" Width="auto" DisplayMemberBinding="{Binding Status}"/>
</GridView>
</ListView.View>
</ListView >
public MainWindow()
{
InitializeComponent();
List<Netadapter> adapters = new List<Netadapter>();
foreach (NetworkInterface netadapter in NetworkInterface.GetAllNetworkInterfaces())
{
Netadapter adapter = new Netadapter(netadapter.Name, netadapter.Description, netadapter.OperationalStatus.ToString());
adapters.Add(adapter);
}
this.DataContext = this;
}
public class Netadapter
{
public string Name { get; set; }
public string Interface { get; set; }
public string Status { get; set; }
public Netadapter(string _name, string _interface, string _status)
{
this.Name = _name;
this.Interface = _interface;
this.Status = _status;
}
}
Your ListView
needs to reference the list of Netadapters
. 您的
ListView
需要引用Netadapters
的列表。
First of all, make the list of adapters a public property: 首先,将适配器列表设为公共属性:
public MainWindow()
{
InitializeComponent();
Adapters = new ObservableCollection<Netadapter>();
// Add adapters
this.DataContext = this;
}
public ObservableCollection<Netadapter> Adapters { get; set; }
Second, bind the public propery to the ListViews
ItemsSource
: 其次,将公共属性绑定到
ListViews
ItemsSource
:
<ListView Grid.Column="0" Grid.ColumnSpan="2" Width="auto" Margin="10" Name="ListView" ItemsSource="{Binding Adapters}" >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.