经过几个月的 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); }
}
}
所以这里是:我使用绑定约定,即 MainView 中的属性应该绑定到视图中同名的元素。
首先,请注意这
PlayerList
只是PlayerProfiles
我创建的玩家名称列表,因为当绑定到 aCombobox
时,当我命名时它们不会出现Combobox PlayerProfiles
,但是当我通过列表并命名时它们会出现PlayerList
- 虽然我已经覆盖ToString
了PlayerProfile class
. 为什么?这看起来很笨拙...(如果您想知道的话,该staticInfos.Load()
方法会填充PlayerProfiles
几个虚拟名称...)当我在文本框中输入内容(称为
TB_AddPlayer
)并按下按钮(称为AddPlayer
)时,会出现测试字符串,所以我知道发生了操作,但新名称没有出现在组合框中此外,如果我使用属性中的注释行
CanAddPlayer
,则永远不会启用该按钮,如果我开始键入,也不会在文本框失去焦点且其中包含文本时。为什么?
抱歉这些基本问题,我已经盯着“Hello World”caliburn 示例看了好几个小时,看不到我遗漏了什么……谢谢。提前!
编辑:这是 .xaml
<UserControl x:Class="MixGameM8_MVVM.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MixGameM8_MVVM.Views">
<UserControl.Resources>
<Style TargetType="TextBlock" x:Key="TB_A">
<Setter Property="Margin" Value="5,5,5,5" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="FontSize" Value="15" />
</Style>
<!-- ... Some more of this kind -->
</UserControl.Resources>
<Grid>
<!-- ... Grid definitions -->
<Border Style="{StaticResource Border_A}" Grid.Row="0" Grid.Column="0">
<StackPanel Name="SP_Controls">
<TextBlock Style="{StaticResource TB_A}" Text="Controls"/>
<ComboBox Style="{StaticResource CB_A}" Name="PlayerProfiles"/>
<StackPanel Orientation="Horizontal">
<TextBox Style="{StaticResource TBox_A}" Name="TB_AddPlayer" MinWidth ="100" />
<Button Style="{StaticResource Button_AddPlayer}" Content="Add Player" Name="AddPlayer" />
</StackPanel>
</StackPanel>
</Border>
<!-- ... And some more cells in the grid, one containing the dummy textbox -->
</Grid>
</UserControl>