Elu*_*FTW 5 wpf binding caliburn.micro
经过几个月的 WPF 实践,我决定试一试 Caliburn Micro。我有几件事无法上班。我将首先发布代码,请参阅下面的问题/问题:
public class MainViewModel : PropertyChangedBase
{
BindableCollection<PlayerProfile> _playerProfiles = staticInfos.Load();
public BindableCollection<PlayerProfile> PlayerProfiles {
get { return _playerProfiles; }
set {
_playerProfiles = value;
NotifyOfPropertyChange(() => PlayerList);
}
}
string _tb_AddPlayer;
public string TB_AddPlayer {
get { return _tb_AddPlayer; }
set {
_tb_AddPlayer = value;
NotifyOfPropertyChange(() => TB_AddPlayer);
NotifyOfPropertyChange(() => CanAddPlayer);
}
}
List<string> _pl = new List<string>();
public List<string> PlayerList {
get{
foreach (PlayerProfile p in PlayerProfiles) {
if (!_pl.Contains(p.Name)) _pl.Add(p.Name);
}
return _pl;
}
set {
_pl = value;
NotifyOfPropertyChange(()=>PlayerList);
}
}
// Dummy Test String
string _test;
public string Test {
get { return _test; }
set {
_test = value;
NotifyOfPropertyChange(() => Test);
}
}
// Button Action
public void AddPlayer() {
_playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
Test = "Pressed";
NotifyOfPropertyChange(() => PlayerProfiles);
}
public bool CanAddPlayer {
get { return true; }
// doesnt work: get { return !string.IsNullOrWhiteSpace(TB_AddPlayer); }
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里是:我使用绑定约定,即 MainView 中的属性应该绑定到视图中的同名元素。
首先,请注意这PlayerList
只是PlayerProfiles
我创建的球员姓名的列表,因为当绑定到 a 时Combobox
,当我命名时他们不会出现Combobox PlayerProfiles
,但是当我通过列表并命名时他们会出现PlayerList
- 虽然我已经覆盖ToString
了PlayerProfile class
. 为什么?这看起来很笨拙......(该staticInfos.Load()
方法填充了PlayerProfiles
几个虚拟名称,如果你想知道......)
当我在文本框中键入内容(称为TB_AddPlayer
)并按下按钮(称为AddPlayer
)时,会出现测试字符串,因此我知道发生了操作,但新名称未出现在组合框中
此外,如果我在CanAddPlayer
属性中使用注释行,则该按钮永远不会启用,如果我开始输入,也不会在文本框与其中的文本失去焦点时启用。为什么?
抱歉,对于这些基本问题,我已经盯着“Hello World”caliburn 示例看了好几个小时,但没有看到我错过了什么……谢谢。提前!
编辑:这是 .xaml
public class MainViewModel : PropertyChangedBase
{
BindableCollection<PlayerProfile> _playerProfiles = staticInfos.Load();
public BindableCollection<PlayerProfile> PlayerProfiles {
get { return _playerProfiles; }
set {
_playerProfiles = value;
NotifyOfPropertyChange(() => PlayerList);
}
}
string _tb_AddPlayer;
public string TB_AddPlayer {
get { return _tb_AddPlayer; }
set {
_tb_AddPlayer = value;
NotifyOfPropertyChange(() => TB_AddPlayer);
NotifyOfPropertyChange(() => CanAddPlayer);
}
}
List<string> _pl = new List<string>();
public List<string> PlayerList {
get{
foreach (PlayerProfile p in PlayerProfiles) {
if (!_pl.Contains(p.Name)) _pl.Add(p.Name);
}
return _pl;
}
set {
_pl = value;
NotifyOfPropertyChange(()=>PlayerList);
}
}
// Dummy Test String
string _test;
public string Test {
get { return _test; }
set {
_test = value;
NotifyOfPropertyChange(() => Test);
}
}
// Button Action
public void AddPlayer() {
_playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
Test = "Pressed";
NotifyOfPropertyChange(() => PlayerProfiles);
}
public bool CanAddPlayer {
get { return true; }
// doesnt work: get { return !string.IsNullOrWhiteSpace(TB_AddPlayer); }
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我将从删除您的财产开始PlayerList
,然后PlayerProfiles
像这样更新您的财产:
public BindableCollection<PlayerProfile> PlayerProfiles {
get { return _playerProfiles; }
set {
_playerProfiles = value;
NotifyOfPropertyChange(() => PlayerProfiles);
}
}
Run Code Online (Sandbox Code Playgroud)
接下来,我不会将您的视图模型属性称为描述它们如何呈现的东西,即更改TB_AddPlayer
为 just AddPlayer
(或者更好的是类似PlayerName
的东西)。
接下来,如果您调用 ComboBox PlayerProfiles
,则绑定应该通过约定自动发生。在 AddPlayer 方法中,您希望通过属性而不是支持字段添加新的玩家配置文件:
改变:
public void AddPlayer() {
_playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
Test = "Pressed";
NotifyOfPropertyChange(() => PlayerProfiles);
}
Run Code Online (Sandbox Code Playgroud)
到:
public void AddPlayer() {
this.PlayerProfiles.Add(new PlayerProfile(this.PlayerName));
}
Run Code Online (Sandbox Code Playgroud)
由于 PlayerProfiles 是 BindableCollection,因此将执行更改通知,并且您的 ComboBox 将更新。
另外,我总是会明确指定支持字段的访问修饰符,即private string _playerName;
不仅仅是字符串_playerName;
尝试这些更改,如果仍有问题,请更新问题中的代码。
归档时间: |
|
查看次数: |
10563 次 |
最近记录: |