【发布时间】:2014-07-21 03:39:08
【问题描述】:
我需要一些帮助来解决我遇到的一个简单的 XAML 绑定问题。
我使用 MVVM 模式和 2 个类。卡和板。我已经用卡片创建了一个模型。 Card 类仅由两个属性组成。
- 我卡的价值。
- 我的卡的状态(下行或上行)。该板包含一张卡片列表。
public class Card
{
public int Value { get; private set; }
public bool Upside { get; set; }
public Card(int value)
{
this.Value = value;
}
}
class Board
{
private List<Card> _cardList = new List<Card>();
public List<Card> CardList
{
get
{
return _cardList;
}
set
{
if (_cardList == value)
{
return;
}
_cardList = value;
raisePropertyChanged("CardList");
}
}
public void Initialize()
{
// Initialization of the card list
}
public void FlipCard()
{
// The rules and actions of my game
}
}
之后,我将所有INotifyPropertyChanged 内容添加到我的卡片和棋盘属性中。我不会向您展示代码以尽可能保持清晰。
在我的ViewModel 我只公开这个:
public Board GameBoard { get; set; }
当然,我还有很多其他的方法、属性和命令,但没有关于卡片的。我的游戏规则和我的牌都在我的棋盘课上。我的 ViewModel 只公开特定于我的 XAML 设计的类。
在我的 XAML 文件中,我有类似的内容。卡片是一个简单的矩形。我只需要用正确的转换器将我的卡的值与 Fill 属性绑定。我的想法是我只公开我的董事会,所以我需要写一些类似的东西
Fill="{Binding **GameBoard.CardList[0]**
我不在运行时生成矩形。现在我的板上有 4 张卡,然后我写了 4 次矩形代码并硬编码了 4 次以 01、02、03、04 结尾的名称。我在这里只给你看 01:
<Window.Resources>
<me:CardFillConverter x:Key="CardConverter"/>
</Window.Resources>
<Rectangle x:Name="Card01" Fill="{Binding ??? Converter={StaticResource CardConverter}}" ... />
什么是正确的绑定?
【问题讨论】:
-
@Alex 感谢您的更正