使用转换器将Silverlight TabControl绑定到复杂对象
|
我试图将我的数据绑定到选项卡控件。我的标题显示得很好,但是我不确定如何根据下面显示的项目模板使选项卡的内容正确绑定。
我认为我在创建选项卡项目时会丢失某些东西,但是我不确定如何将MyCustomObject绑定到每个TabItem。
XAML:
<sdk:TabControl ItemsSource=\"{Binding Singles,Converter={StaticResource TabConverter}}\">
<sdk:TabControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation=\"Horizontal\" />
</ItemsPanelTemplate>
</sdk:TabControl.ItemsPanel>
<sdk:TabControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBox Text=\"{Binding Converter={StaticResource RoundNumberConverter}}\" Margin=\"2\" />
<ListBox x:Name=\"Matches\" ItemsSource=\"{Binding}\" Margin=\"2\">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=\"200\" />
<ColumnDefinition Width=\"200\" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row=\"0\" Grid.Column=\"0\" Text=\"{Binding Converter={StaticResource SeedingConverter}, ConverterParameter=true}\" Margin=\"2\" />
<TextBlock Grid.Row=\"1\" Grid.Column=\"0\" Text=\"{Binding Converter={StaticResource SeedingConverter}, ConverterParameter=false}\" Margin=\"2\" />
<TextBlock Grid.Row=\"0\" Grid.Column=\"1\" Text=\"{Binding Path=Player1Name}\" Margin=\"2\" />
<TextBlock Grid.Row=\"1\" Grid.Column=\"1\" Text=\"{Binding Path=Player2Name}\" Margin=\"2\" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
</DataTemplate>
</sdk:TabControl.ItemTemplate>
</sdk:TabControl>
转换器:
public class TabConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
IEnumerable<IGrouping<string, MyCustomObject>> source = value as IEnumerable<IGrouping<string, MyCustomObject>>;
if (source != null)
{
var controlTemplate = (ControlTemplate)parameter;
List<TabItem> result = new List<TabItem>();
foreach (IGrouping<string, MyCustomObject> tab in source)
{
result.Add(new TabItem()
{
Header = tab.Key,
DataContext = tab //not sure this is right?
});
}
return result;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
没有找到相关结果
已邀请:
2 个回复
皇小福另届
注意:这要求您将ViewModel作为参数传递给TabConverter,例如:
然后,在新控件的每个实例中都有ViewModel时,请相应地调整绑定! 请注意,诀窍是您对Selected对象的单个实例具有单独的绑定
辟官陡板休