我有一類這樣的: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; }
}
我的XAML看起來是這樣的:
<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
集合中的ContestTeam
,該集合的值與ScheduleViewModel.TeamId
的值相匹配?
我最後的追索權將只使用Loaded
事件StackPanel
來設置它在代碼隱藏DataContext
,但我想避免這種情況。有沒有任何XAML Ninjas可以幫我解決這個問題?
您可以創建一個'IValueConverter'其派生自'DependencyObject',並有一些'DependencyProperty',它保存'Silverlight'的'Id'。 – Mohsen 2012-10-08 09:19:12