你好学习MVVM,我有点困惑,我试图在列表中显示我的模型名称,这里是我的。
模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
namespace Morza.Model
{
public class DBEnvironment : INotifyPropertyChanged
{
private String _Name;
private String _HostName;
private String _HostPath;
public DBEnvironment(String __name, String __hostname, String __hostpath)
{
Name = __name;
HostName = __hostname;
HostPath = __hostpath;
}
public String Name {
get
{
return _Name;
}
set
{
_Name = value;
OnPropertyChanged("Name");
}
}
public String HostName
{
get
{
return _HostName;
}
set
{
_HostName = value;
OnPropertyChanged("HostName");
}
}
public String HostPath
{
get
{
return _HostPath;
}
set
{
_HostPath = value;
OnPropertyChanged("HostPath");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
模型视图
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Morza.Model;
using System.Collections.ObjectModel;
namespace Morza.ViewModel
{
internal class LoginViewModel : ViewModelBase
{
ObservableCollection<DBEnvironment> _DBEnvironments;
private DBEnvironment _obj;
public LoginViewModel()
{
DBEnvironmentInfo = new ObservableCollection<DBEnvironment>();
DBEnvironmentInfo.Add(new DBEnvironment("T2", "127.0.0.1", "C:"));
DBEnvironmentInfo.Add(new DBEnvironment("T2", "127.0.0.1", "C:"));
DBEnvironmentInfo.Add(new DBEnvironment("T2", "127.0.0.1", "C:"));
DBEnvironmentInfo.Add(new DBEnvironment("T2", "127.0.0.1", "C:"));
}
public DBEnvironment DBEnvironment {
get
{
return _obj;
}
set
{
_obj = value;
}
}
public ObservableCollection<DBEnvironment> DBEnvironmentInfo
{
get
{
return _DBEnvironments;
}
set
{
_DBEnvironments = value;
OnPropertyChanged("PersonsInfo");
}
}
}
}
查看
<Window x:Class="Morza.View.Login"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Login" Height="260" Width="425" WindowStartupLocation="CenterScreen" Background="LightBlue" ResizeMode="NoResize">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="10"></RowDefinition> <!-- 0 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 1 Label Instruction -->
<RowDefinition Height="10"></RowDefinition> <!-- 2 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 3 Username -->
<RowDefinition Height="10"></RowDefinition> <!-- 4 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 5 Password -->
<RowDefinition Height="10"></RowDefinition> <!-- 6 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 7 Environment -->
<RowDefinition Height="10"></RowDefinition> <!-- 8 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 9 Label Warning -->
<RowDefinition Height="10"></RowDefinition> <!-- 10 Nothing -->
<RowDefinition Height="25"></RowDefinition> <!-- 11 Button-->
<RowDefinition Height="10"></RowDefinition> <!-- 12 Nothing -->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="25"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Name="lblInstruction" Content="Enter a Username, Password, and Environment" Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center"></Label>
<Label Name="lblUsername" Grid.Column="1" Grid.Row="3" Content="Username:" HorizontalAlignment="Right"></Label>
<TextBox Name="txtUsername" Grid.Column="3" Grid.Row="3"></TextBox>
<Label Name="lblPassword" Grid.Column="1" Grid.Row="5" Content="Password:" HorizontalAlignment="Right"></Label>
<PasswordBox Name="pwdPassword" Grid.Column="3" Grid.Row="5"></PasswordBox>
<Label Name="lblEnv" Grid.Column="1" Grid.Row="7" Content="Environment:" HorizontalAlignment="Right"></Label>
<ListView Grid.Column="1" Grid.ColumnSpan="3" Grid.Row="7" ItemsSource="{Binding DBEnvironmentInfo}" >
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding DBEnvironmentInfo.Name}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Name="lblWarning" Content="" Grid.Column="1" Grid.ColumnSpan="3" Foreground="Red" Grid.Row="9" HorizontalAlignment="Center"></Label>
<Button Name="btnLogin" Content="Login" Grid.Column="1" Grid.Row="11"></Button>
<Button Name="btnCancel" Content="Exit" Grid.Column="3" Grid.Row="11" IsCancel="True"></Button>
</Grid>
</Window>
运行此代码时,问题是网格是空白的。我没有使用任何我知道但并非真正想要的框架。如果有我遗失的东西。
答案 0 :(得分:1)
看起来你忘了设置Window的DataContext:
<Window
...
xmlns:ViewModels="clr-namespace:Morza.ViewModel">
<Window.DataContext>
<ViewModels:LoginViewModel />
</Window.DataContext>
...
另外,另外要提到的是您不需要在ListView DataTemplate中添加属性名称:
<Label Content="{Binding Name}" />
这是因为您的DataTemplate绑定到列表中的单个对象。