我有一个ListView
绑定到ObservableCollection
。当我的按钮命令更新集合中的项目时,视图不会更新。我也在使用Fody来实现INotifyPropertyChanged
。
查看
<ListView ItemsSource="{Binding Path=DatabaseInfos}" VerticalAlignment="Stretch" Margin="10">
<ListView.View>
<GridView>
<GridViewColumn Header="Current Version">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding CurrentVerion}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Content="Test"
Command="{Binding DataContext.CmdButtonClicked, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
CommandParameter="{Binding}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
视图模型
public interface IDatabaseViewModel
{
ObservableCollection<DatabaseInfo> DatabaseInfos { get; set; }
}
[Injectable(InstanceScope.Singleton)]
[ImplementPropertyChanged]
public class DatabaseViewModel : IDatabaseViewModel
{
private RelayCommand _buttonClicked;
private ILogger _logger;
public DatabaseViewModel()
{
_logger = ServiceLocator.Default.GetInstance<ILoggerFactory>().GetLogger(this);
DatabaseInfos = new ObservableCollection<DatabaseInfo>();
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\CampusInstallerSettings.xml";
if (File.Exists(path))
{
try
{
var settings = SettingsReader.LoadSettings(path);
foreach (var db in settings.DatabaseSettings.DatabaseSettings)
{
var dbInfo = new DatabaseInfo();
if (db.ActionCode.Value == "Upgrade")
dbInfo.Action = Action.Upgrade;
else if (db.ActionCode.Value == "None")
dbInfo.Action = Action.None;
else if (db.ActionCode.Value == "Remove")
dbInfo.Action = Action.Uninstall;
else
dbInfo.Action = Action.None;
dbInfo.SqlServer = db.SQLServer.Value;
dbInfo.Database = db.Database.Value;
if (db.DBType.Value == "CampusVue")
dbInfo.DatabaseType = DatabaseType.CampusVue;
if (db.Connection.Value == "Integrated")
dbInfo.IntegratedSecurity = true;
DatabaseInfos.Add(dbInfo);
}
}
catch (Exception ex)
{
_logger.Error(ex);
throw new Exception("Could not load settings file. " + Environment.NewLine + ex.Message);
}
}
else
{
throw new Exception("Could not find settings file @ " + path);
}
}
public ICommand CmdButtonClicked
{
get { return _buttonClicked ?? (_buttonClicked = new RelayCommand(ButtonClicked)); }
}
public ObservableCollection<DatabaseInfo> DatabaseInfos { get; set; }
private void ButtonClicked(object o)
{
_logger.Info(@"Test button clicked.");
var dbInfo = o as IDatabaseInfo;
if (dbInfo != null && !dbInfo.TestConnection())
{
MessageBox.Show("Couldn't connect", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
模型
public interface IDatabaseInfo
{
Action Action { get; set; }
string SqlServer { get; set; }
string Database { get; set; }
DatabaseType DatabaseType { get; set; }
bool IntegratedSecurity { get; set; }
string Username { get; set; }
string Password { get; set; }
string CurrentVersion { get; set; }
bool TestConnection();
}
[ImplementPropertyChanged]
public class DatabaseInfo : IDatabaseInfo
{
public Action Action { get; set; }
public string SqlServer { get; set; }
public string Database { get; set; }
public DatabaseType DatabaseType { get; set; }
public bool IntegratedSecurity { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string CurrentVersion { get; set; }
public bool TestConnection()
{
if (DbHelper.TestConnection(SqlServer, 1433, Database, Username, Password))
{
CurrentVersion = DbHelper.GetDbVersion(SqlServer, 1433, Database, Username, Password);
return true;
}
return false;
}
}
答案 0 :(得分:2)
您没有更新该属性。
public string CurrentVersion { get; set; }
是您要更新的内容。由于您未在设置器中调用OnPropertyChanged
,因此TextBox不会更新为新版本。
private string _currentVersion = string.Empty;
public string CurrentVersion
{
get { return _currentVersion };
set
{
_currentVersion = value;
OnPropertyChanged("CurrentVersion");
}
}
我猜你的DatabaseInfo就是你的Model;)。在这里您可以选择创建一个返回/设置更新值的小型ViewModel包装类,以便将INPC保留在Model层之外。否则你可以使用上面的代码片段:)
修改强>
在这种情况下,只需拼写正确;)
TextBox Text =&#34; {Binding CurrentVerion }&#34;