经过几个月的动手WPF,我决定给Caliburn Micro一枪。我有几件事情我无法工作。我先发布代码,请参阅下面我的问题/问题: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
的名字,这是我创建,因为结合到Combobox
的时候,他们不露面,当我命名Combobox PlayerProfiles
的名单,但当我通过列表并将其命名为PlayerList
时 - 虽然我已覆盖PlayerProfile class
中的ToString
方法。为什么?这似乎笨拙...(该staticInfos.Load()
方法填充PlayerProfiles
几个假名字,如果你想知道...)当我在文本框中键入(称为
TB_AddPlayer
)的东西,然后按下按钮(称为AddPlayer
)测试字符串出现,所以我知道这个动作发生了,但是新名字没有出现在组合框中,但是新名字没有出现在组合框中,但是如果我使用CanAddPlayer
属性中的注释行,该按钮从不被启用,而不是如果我开始输入,也不会在文本框失去焦点时使用文本。为什么?
对不起,对于这些基本问题,我已经盯着'Hello World'caliburn示例几个小时,并没有看到我缺少的东西...... Thx。提前!
编辑:这里是的.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>
你可以请你也发布你的'MainView' XAML? – nemesv