我正在尝试将具有多个属性的列表加载到列表视图的gridview列中。我确实设置了一个模型类,将网络适配器放入列表中,并且还在xaml代码中写下了绑定。它仍然无法正常工作。
我很可能忘记了执行步骤或使用了错误的逻辑。我也尝试研究其他线程,但是似乎没有一个线程可以解决我遇到的问题。感谢您的帮助。
<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;
}
}
答案 0 :(得分:2)
您的ListView
需要引用Netadapters
的列表。
首先,将适配器列表设为公共属性:
public MainWindow()
{
InitializeComponent();
Adapters = new ObservableCollection<Netadapter>();
// Add adapters
this.DataContext = this;
}
public ObservableCollection<Netadapter> Adapters { get; set; }
第二,将公共属性绑定到ListViews
ItemsSource
:
<ListView Grid.Column="0" Grid.ColumnSpan="2" Width="auto" Margin="10" Name="ListView" ItemsSource="{Binding Adapters}" >