Wpf Caliburn Micro,绑定和通知

Wpf Caliburn Micro,绑定和通知,wpf,binding,caliburn.micro,Wpf,Binding,Caliburn.micro,经过几个月的WPF实践,我决定给Caliburn Micro打一针。我有几件事不能去上班。我将首先发布代码,请参见下面我的问题: public class MainViewModel : PropertyChangedBase { BindableCollection<PlayerProfile> _playerProfiles = staticInfos.Load(); public BindableCollection<PlayerProfile>

经过几个月的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); }
    }
}

首先,我将首先删除您的
PlayerList
属性,并更新您的
PlayerProfiles
属性,如下所示:

 public BindableCollection<PlayerProfile> PlayerProfiles {
    get { return _playerProfiles; }
    set { 
        _playerProfiles = value;
        NotifyOfPropertyChange(() => PlayerProfiles);
    }
}
致:

由于PlayerProfiles是BindableCollection,因此将执行更改通知,并更新您的组合框

此外,我总是明确指定支持字段的访问修饰符,即
private string\u playerName不仅仅是字符串
\u playerName


尝试这些更改,如果仍有问题,请更新问题中的代码。

我想我已经找到了答案

在文本框上使用updatesourcetrigger=属性已更改 Can Execute未更新,因为您没有使用dependens-on(或dependencies-don-member-The-name)属性


组合框未更新,因为您应该将其绑定到可绑定的集合。

您是否也可以发布您的
MainView
XAML?谢谢您的回答!但是,当我直接绑定到“PlayerProfiles”时(我在通知中做了更改),组合框不显示名称,而是两次显示(有两个由Load()方法生成的配置文件)条目“找不到”Project_MVVM.Models.PlayerProfile“的视图”。这是什么意思?当然,我想如果我写“字符串a…”和“私有字符串a…”它们是以完全相同的方式编译的,即省略前缀会自动将其设置为private,还是我错了?您是对的,但最好是显式地添加“private”访问修饰符。对于第一个问题,默认情况下,Caliburn.Micro将假定集合中的类型是视图模型,并将尝试查找该类型的关联视图(例如,PlayerProfile.xaml),并将PlayerProfile类型绑定到PlayerProfile视图)。但是,由于这不是视图模型类型,您可以将ComboBox的“DisplayMemberPath”属性设置为PlayerProfile类型的属性,或者创建ComboBox ItemTemplate。有关ItemTemplate的示例,请参阅。如果您想在组合框中显示多个字符串,则可以使用此方法。因此Caliburn在显示对象时默认不使用ToString方法,这很好!我想我也可以创建一个新的.xaml“包含该模板”,但作为PlayerProfile模型的视图,而不是模板?对于所有文本框实例,更新源触发器默认设置为使用Caliburn.Micro更改的属性。不过,我无法使该部分正常工作。现在,当按下按钮时,我正在向组合框添加一个新的播放器,但无论文本框的内容是什么,播放器都没有名称(即,组合框选项中会出现更多的白线)。。。为什么?有人想到了,文本框绑定仍然不起作用:-(…一切仍然和上一条评论一样。
public void AddPlayer() {
     _playerProfiles.Add(new PlayerProfile(TB_AddPlayer));
     Test = "Pressed";
     NotifyOfPropertyChange(() => PlayerProfiles);
}
public void AddPlayer() {
     this.PlayerProfiles.Add(new PlayerProfile(this.PlayerName));
}