我正在嘗試實作一個日期選擇器并將其系結到我創建的模型。我在 App.xaml.cs 中創建了一個全球錦標賽物件:
private Tournament _tournament;
public Tournament tournament {
get { return _tournament; }
set { _tournament = value; OnPropertyChanged("tournament"); }
}
我做了一個 OnStartup 覆寫來啟動我的視窗:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
//setup score
score = new Score();
// Tournament setup
tournament = new Tournament();
tournament.GamesToWin = 1;
tournament.Games = new List<Game>(1);
tournament.Players = new List<Player>(2) { new Player(), new Player() };
//tournament.TimeAndDate = new DateTime(2021, 11, 22);
tournament.Winner = null;
// Initializing the UserInput
UserInput userInput = new UserInput();
UserInputWindowViewModel userinputViewModel = new UserInputWindowViewModel();
userInput.DataContext = userinputViewModel;
// Opening the UserInput Window
bool? res = userInput.ShowDialog();
// If the UserInput Window is closed, open the next Window
if (res == true)
{
// Opening the MainWindow
MainWindow main = new MainWindow();
main.Show();
}
else
{
Shutdown();
}
}
模型.cs(錦標賽.cs):
public class Tournament : INotifyPropertyChanged
{
private Player _winner;
private DateTime? _timeAndDate;
private List<Player> _players;
public List<Player> Players
{
get { return _players; }
set { _players = value; OnPropertyChanged("Players"); }
}
private List<Game> _games;
public List<Game> Games
{
get { return _games; }
set { _games = value; OnPropertyChanged("Games"); }
}
private int _gamesToWin;
public int GamesToWin
{
get { return _gamesToWin; }
set { _gamesToWin = value; OnPropertyChanged("GamesToWin"); }
}
public Player Winner
{
get { return _winner; }
set { _winner = value; OnPropertyChanged("Winner"); }
}
public DateTime? TimeAndDate
{
get { return _timeAndDate; }
set { _timeAndDate = value; OnPropertyChanged("TimeAndDate"); }
}
#region PropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
ViewModel.cs (UserInputWindowViewModel.cs):
public class UserInputWindowViewModel
{
// Calling the current app to access the tournament object globally
public App currentApp = Application.Current as App;
#region The players object
private List<Player> _players;
public List<Player> Players
{
get { return _players; }
set { _players = value; }
}
#endregion
#region The DateTime object
private DateTime? _dateTime;
public DateTime? TournamentDateTime
{
get { return _dateTime; }
set { _dateTime = value; }
}
#endregion
public UserInputWindowViewModel()
{
Tournament tournament = currentApp.tournament;
Players = tournament.Players;
TournamentDateTime = new DateTime(2021, 11, 22);
tournament.TimeAndDate = TournamentDateTime;
//TournamentDateTime = tournament.TimeAndDate;
}
#region ICommand Members
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if (mUpdater == null)
mUpdater = new Updater();
return mUpdater;
}
set
{
mUpdater = value;
}
}
private class Updater : ICommand
{
public bool CanExecute(object parameter) => true;
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
}
}
#endregion
}
在我的視窗 (UserInput.xaml) 中,我有以下 Datepicker:
<DatePicker SelectedDate="{Binding TournamentDateTime, Mode=TwoWay}"/>
我已將視圖模型中的日期設定為 22-11-2021 作為測驗,但是當我更改日期時,它并沒有改變。我究竟做錯了什么?
編輯:我還嘗試了 ViewModel 中的 OnPropertyChanged,但沒有奏效
uj5u.com熱心網友回復:
你忘記INotifyPropertyChanged在UserInputWindowViewModel.cs.
public class UserInputWindowViewModel : INotifyPropertyChanged
{
}
還要確保您PropertyChanged在屬性設定器中觸發事件,這對于雙向系結很重要
public DateTime? TournamentDateTime
{
get { return _dateTime; }
set
{
_dateTime = value;
OnPropertyChanged(nameof(TournamentDateTime);
}
}
我還建議不要Application直接從視圖模型訪問,最好tournament盡快將物件作為依賴項傳遞給視圖模型,這樣可以更好地分離關注點
uj5u.com熱心網友回復:
我設法解決了我的問題:我做了一個小函式來更新我的全域模型:
public void updateModel(DateTime? s)
{
currentApp.tournament.TimeAndDate = s;
}
我在視圖模型中的 getter/setter 中添加了一小段代碼(注意我使用 DateTime.Now 將其設定為當前日期):
private DateTime? _dateTime = DateTime.Now;
public DateTime? TournamentDateTime
{
get { return _dateTime; }
set { _dateTime = value; updateModel(value); }
}
這解決了我使用日期選擇器的所有問題。而且我不必在我的視圖模型中使用 INotifyPropertychanged,因為我的模型已經實作了它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/337962.html
上一篇:如何為控制臺和WPF.NET應用程式的PythonNET設定Runtime.PythonDLL(PIP安裝)
下一篇:更改helix工具包加載的STL模型的顏色(材質)。無法從Model3DGroup轉換為GeometryModel3D
