使用转换器将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();
        }

    }
    
已邀请:
        您到达那里的大部分时间,以下对我有用: 剥离DataTemplate并将其内容直接放到新的UserControl中,在此示例中,将其称为MatchesView。 然后在TabConverter中,将foreach循环的内容修改为如下所示:
TabItem tabitem = new TabItem();
tabitem.Header = tab.Key;

MatchesView tabview = new MatchesView();
tabview.DataContext = parameter;

tabitem.Content = tabview;
result.Add(tabitem);
注意:这要求您将ViewModel作为参数传递给TabConverter,例如:
<sdk:TabControl SelectedItem=\"{Binding YourSelectedObject}\" ItemsSource=\"{Binding YourCollectionObject, Converter={StaticResource TabConverter}, ConverterParameter={StaticResource YourViewModel}, Mode=TwoWay}\" />
然后,在新控件的每个实例中都有ViewModel时,请相应地调整绑定! 请注意,诀窍是您对Selected对象的单个实例具有单独的绑定     
        检查您的问题,我认为没有任何支持。 WPF的TabItem包含ItemTemplate和ContentTemplate。第一个是Header的模板,第二个是\“ body \”的模板。在Silverlight中,我们仍然没有ContentTemplate。还没有看到正式的声明,但是这个家伙说直到SL5才会得到支持。     

要回复问题请先登录注册