XAML:绑定到集合中的单个子级

XAML:绑定到集合中的单个子级,xaml,silverlight-3.0,Xaml,Silverlight 3.0,我有一门课是这样的: public class Contest { List<ContestTeam> Teams { get; set; } } public class ContestTeam { int TeamId { get; set; } int FinalScore { get; set; } } public class ScheduleViewModel { int CurrentTeamId { get; } List&

我有一门课是这样的:

public class Contest {
    List<ContestTeam> Teams { get; set; }
}

public class ContestTeam {
    int TeamId { get; set; }
    int FinalScore { get; set; }
}
public class ScheduleViewModel {
    int CurrentTeamId { get; }
    List<Contest> Schedule { get; }
}
<ListBox ItemsSource="{Binding Path=Schedule}">
    <ListBox.ItemTemplate>
        <DataTemplate>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <!-- TODO: DataContext is currently hard coded to 425 - this needs to be fixed -->
                    <StackPanel Orientation="Horizontal" 
                            DataContext="{Binding Path=Teams, Converter={StaticResource CurrentTeam}, ConverterParameter=425}">

                        <TextBlock Text="{Binding SomeProperty}" />
                    </StackPanel>

                    <Button Content="Delete" />
                </StackPanel>

                <ListBox Grid.Row="1" ItemsSource="{Binding Teams}">
                    <!-- a list of all the teams -->
                </ListBox>
            </Grid>

        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
公开课竞赛{
列出团队{get;set;}
}
公开课竞赛队{
int TeamId{get;set;}
int FinalScore{get;set;}
}
我的视图模型如下所示:

public class Contest {
    List<ContestTeam> Teams { get; set; }
}

public class ContestTeam {
    int TeamId { get; set; }
    int FinalScore { get; set; }
}
public class ScheduleViewModel {
    int CurrentTeamId { get; }
    List<Contest> Schedule { get; }
}
<ListBox ItemsSource="{Binding Path=Schedule}">
    <ListBox.ItemTemplate>
        <DataTemplate>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <!-- TODO: DataContext is currently hard coded to 425 - this needs to be fixed -->
                    <StackPanel Orientation="Horizontal" 
                            DataContext="{Binding Path=Teams, Converter={StaticResource CurrentTeam}, ConverterParameter=425}">

                        <TextBlock Text="{Binding SomeProperty}" />
                    </StackPanel>

                    <Button Content="Delete" />
                </StackPanel>

                <ListBox Grid.Row="1" ItemsSource="{Binding Teams}">
                    <!-- a list of all the teams -->
                </ListBox>
            </Grid>

        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
公共类ScheduleViewModel{
int CurrentTeamId{get;}
列表计划{get;}
}
我的XAML看起来像这样:

public class Contest {
    List<ContestTeam> Teams { get; set; }
}

public class ContestTeam {
    int TeamId { get; set; }
    int FinalScore { get; set; }
}
public class ScheduleViewModel {
    int CurrentTeamId { get; }
    List<Contest> Schedule { get; }
}
<ListBox ItemsSource="{Binding Path=Schedule}">
    <ListBox.ItemTemplate>
        <DataTemplate>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition  />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal">
                    <!-- TODO: DataContext is currently hard coded to 425 - this needs to be fixed -->
                    <StackPanel Orientation="Horizontal" 
                            DataContext="{Binding Path=Teams, Converter={StaticResource CurrentTeam}, ConverterParameter=425}">

                        <TextBlock Text="{Binding SomeProperty}" />
                    </StackPanel>

                    <Button Content="Delete" />
                </StackPanel>

                <ListBox Grid.Row="1" ItemsSource="{Binding Teams}">
                    <!-- a list of all the teams -->
                </ListBox>
            </Grid>

        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

基本上,我可以继续开发我创建了一个值转换器(CurrentTeam),并将
TeamId
硬编码为
ConverterParameter
,以便继续开发视图。但我有点陷入僵局。是否有方法(使用XAML)将
StackPane
l的
DataContext
绑定到
Teams
集合中与
ScheduleViewModel.TeamId
的值匹配的
Context


我最后的办法是使用
StackPanel
Loaded
事件将其设置为
DataContext
,但我希望避免这种情况。有没有XAML忍者可以帮我解决这个问题?

没有办法在XAML绑定中进行查询。在这种情况下,因为您确实有两个输入值(
Teams
CurrentTeamId
),所以只需使用
MultiBinding
IMultiValueConverter

public class FindContestByIdConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType,
                          object parameter, CultureInfo culture)
    {
        var teams = (IEnumerable<ContestTeam>)values[0];
        var teamId = (int)values[1];
        return teams.Single(t => t.TeamId == teamId);
    }

    // ConvertBack that throws NotSupportedException
    ...
}
公共类FindContestByIdConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,
对象参数,CultureInfo(区域性)
{
var团队=(IEnumerable)值[0];
var teamId=(int)值[1];
返回团队。单个(t=>t.TeamId==TeamId);
}
//抛出NotSupportedException的ConvertBack
...
}
然后是XAML:

<StackPanel>
  <StackPanel.DataContext>
    <MultiBinding Converter="{StaticResource FindContestByIdConverter}">
      <Binding Path="Teams"/>
      <Binding Path="CurrentTeamId" />
    </MultiBinding>
  </StackPanel.DataContext>
  ...
</StackPanel>

...

Silverlight还不支持多重绑定-但是我找到了这个链接:如何实现大多数多重绑定的
ConvertBack
,实际上没有办法从最后一个值中重新创建原来的两个(或更多)值,所以您只需抛出NotSupportedException,就是这样。如果您需要双向绑定,您需要准确描述在回答该问题之前它的工作方式。您可以创建一个
IValueConverter
,它派生自
DependencyObject
,并具有一些
dependencProperty
属性,其中包含
Silverlight
Id