【问题标题】:Binding a Silverlight TabControl to a complex object using a converter使用转换器将 Silverlight TabControl 绑定到复杂对象
【发布时间】:2011-06-14 16:25:08
【问题描述】:

我正在尝试将我的数据绑定到选项卡控件。我的标题显示正常,但我不确定如何根据下面显示的项目模板正确绑定选项卡的内容。

我想我在创建选项卡项时遗漏了一些东西,但我不确定如何将 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();
        }

    }

【问题讨论】:

    标签: silverlight silverlight-4.0 binding tabcontrol


    【解决方案1】:

    您已经完成了大部分工作,以下对我有用:

    剥离您的 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 对象的单个实例有单独的绑定

    【讨论】:

      【解决方案2】:

      检查您的问题我认为没有支持。 WPF 的 TabItem 包含 ItemTemplate 和 ContentTemplate。第一个是 Header 的模板,第二个是“body”的模板。在 silverlight 中,我们仍然没有 ContentTemplate。还没看到官方声明,不过this guy 说要到SL5才会支持。

      【讨论】:

        猜你喜欢
        • 2011-08-31
        • 1970-01-01
        • 2011-04-07
        • 1970-01-01
        • 1970-01-01
        • 2021-10-06
        • 2012-01-14
        • 2012-03-10
        • 2011-07-30
        相关资源
        最近更新 更多