我的视图模型
public class ExchangeViewModel : ViewModelBase
{
ObservableCollection<string> _repo = new ObservableCollection<string>()
{
"CME",
"CFE",
"LIFFE"
};
#region Properties
public ObservableCollection<string> Exchanges
{
get
{
return _repo;
}
}
#endregion // Properties
#region Constructors
public ExchangeViewModel() { }
#endregion // Constructors
}
我的观点
<Window x:Class="ListviewTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ListView
Grid.Row="0"
Grid.Column="0"
SelectionMode="Single"
DataContext="{Binding}"
>
<ListView.View>
<GridView>
<GridViewColumn
DisplayMemberBinding="{Binding Exchanges}"
Header = "Exchange"
>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Grid>
我在 app.xaml.cs 中将它们绑定在一起
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MainWindow window = new MainWindow();
// Create the ViewModel to which
// the main window binds.
//string path = "Data/customers.xml";
ExchangeViewModel viewModel = new ExchangeViewModel();
// Allow all controls in the window to
// bind to the ViewModel by setting the
// DataContext, which propagates down
// the element tree.
window.DataContext = viewModel;
window.Show();
}
}
但是当我运行应用程序时,我的列表视图是空的。我的绑定交换属性甚至没有被调用,这表明我的绑定没有正确完成。