1

在此处输入图像描述在此处输入图像描述我有一个具有另一个类的私有成员的类,并且该类是另一个类的 ObserveableCollection ..这是我需要从中获取信息的类,它具有我想要数据绑定的私有成员。

private readonly NflGameCollection _games;
....

class NflGameCollection : ObservableCollectionEx<NflGameStatus>
    {...

class NflGameStatus : INotifyPropertyChanged
    {
    //***these are the members i want databound***
    private readonly string _homeTri;
    private readonly string _awayTri;
    private string _homeScore;
    private string _awayScore;

所以每次检测到游戏时都会弹出这个 NflGameStatus 的多个实例......我知道如何访问它的唯一方法是这样做:

_controller = new DtvGsisDataParser.AppController();
    foreach (var item in _controller.Games)
            {
                string hometri = item.HomeTri;
                string awaytri = item.AwayTri;
        ... etc etc
            }

我怎样才能得到它,以便如果 hometri 和 awaytri 等于我正在寻找的......我可以获得该类的其他实例?例如

if item.HomeTri==what i want && item.AwayTri==what i want
        then bind item._homeScore to a certain label
        then bind item.awayAScore to a certain label.

我知道我所要求的有点复杂.. 但我在这里有点绝望,希望能得到任何帮助。这个数据绑定对我来说很新,我很难掌握它。这甚至可能吗?我研究得越多,我不这么认为..但我希望我不是。谢谢你的帮助

4

2 回答 2

1

您需要将选项卡控件绑定到 ViewModel 集合,每个游戏一个:

<TabControl x:Name="_tabControl" ItemsSource="{Binding GameModels}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding GameTitle}"/>
            <Setter Property="Content" Value="{Binding GameContent}"/>
        </Style>
    </TabControl.ItemContainerStyle>
</TabControl>

ViewModel 可能如下所示:

public class NflGame : ViewModelBase
{
    public String GameTitle;
    public NflGameContent GameContent;
}

每个NflGameContent看起来像:

public class NflGameContent : ViewModelBase
{
    private Thread _worker;

    public NflGameContent(int gameID){
        _worker = new Thread(() => UpdateScores(gameID));
        _worker.Start();
    }

    private UpdateScores(gameID){
       var appController = new DtvGsisDataParser.AppController(); //Or whatever -- you get the idea
       HomeScoreText = appController.GetHomeScore(gameID);
       AwayScoreText = appController.GetAwayScore(gameID);
       OnPropertyChanged("HomeScoreText");
       OnPropertyChanged("AwayScoreText");
    }

    public String HomeScoreText;
    public String AwayScoreText;
}

然后,您的 XAML 当然会绑定到 NflGameContent 并接收更新 OnPropertyChanged:

<TextBlock Text="{Binding HomeScoreText}">
于 2013-10-08T17:26:04.907 回答
1

我的建议是您将控件的 DataContext 设置为您希望显示的游戏:

<StackPanel DataContext="{Binding SelectedGame}">
    <Label Content="{Binding HomeScoreText}" />
    <Label Content="{Binding AwayScoreText}" />
</StackPanel>

所以首先你需要像这样在类上公开HomeScoreTextandAwayScoreText作为公共属性:NflGameStatus

class NflGameStatus
{
    private readonly string _homeTri;
    private readonly string _awayTri;
    private string _homeScore;
    private string _awayScore;

    public String HomeScoreText
    {
        get
        {
            return _homeScore;
        }
    }
    public String AwayScoreText
    {
        get
        {
            return _awayScore;
        }
    }
}

然后你需要像这样在你的 ViewModel 上公开一个公共属性:

private NflGameStatus _selectedGame;
public NflGameStatus SelectedGame
{
    get
    {
        return _selectedGame;
    }
    set
    {
        _selectedGame = value;
        RaisePropertyChanged("SelectedGame");
    }
}

现在,当您设置 SelectedGame 属性时,它将自动更新这些字段。所以只需检查一下:

if item.HomeTri==what i want && item.AwayTri==what i want
    SelectedGame = item;

干杯,埃里克

于 2013-10-08T16:57:24.253 回答