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
。