gpt4 book ai didi

silverlight - 将 Silverlight TabControl 绑定(bind)到集合

转载 作者:行者123 更新时间:2023-12-04 21:25:43 25 4
gpt4 key购买 nike

我的 ViewModel 中有一组模型对象。我希望能够将 TabControl 绑定(bind)到这些并使用 DataTemplate 从模型对象中提取信息。当我尝试这样做时,我收到错误消息:无法将模型类型的对象转换为 TabItem 类型的对象。在花了一些时间寻找解决方案后,我发现了以下内容:

  • Silverlight TabControl 是
    splinter 的。使用 ListBox 的组合
    和 ContentControl 来模仿
    TabControl 的行为。 (方法
    我必须对 ListBox 进行皮肤处理
    看起来像一个 TabControl)
  • TabControl 不会覆盖
    PrepareContainerForItemOverride 和
    解决方案是制作一个
    转换器。 (不太好,因为我
    然后需要指定类型
    转换器中的转换者)

  • 有人知道更好的解决方案吗?

    XAML
    <sdk:TabControl ItemsSource="{Binding Items, ElementName=MyControl}">
    <sdk:TabControl.ItemTemplate>
    <DataTemplate>
    <TextBlock Text="{Binding Name}" />
    </DataTemplate>
    </sdk:TabControl.ItemTemplate>
    </sdk:TabControl>

    C#
    public ObservableCollection<Model> Items { get; set; }

    public ViewModel()

    Items = new ObservableCollection<Model>{
    new Model { Name = "1"},
    new Model { Name = "2"},
    new Model { Name = "3"},
    new Model { Name = "4"}
    };
    }

    Suggested Converter :
    public class TabConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    List<TabSource> source = value as List<TabSource>;
    if (source != null)
    {
    List<TabItem> result = new List<TabItem>();
    foreach (TabSource tab in source)
    {
    result.Add(new TabItem()
    {
    Header = tab.Header,
    Content = tab.Content
    });
    }
    return result;
    }
    return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

    最佳答案

    创建转换器

    public class SourceToTabItemsConverter : IValueConverter
    {
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    try
    {
    var source = (IEnumerable)value;
    if (source != null)
    {
    var controlTemplate = (ControlTemplate)parameter;

    var tabItems = new List<TabItem>();

    foreach (object item in source)
    {
    PropertyInfo[] propertyInfos = item.GetType().GetProperties();

    //тут мы выбираем, то поле которое будет Header. Вы должны сами вводить это значение.
    var propertyInfo = propertyInfos.First(x => x.Name == "name");

    string headerText = null;
    if (propertyInfo != null)
    {
    object propValue = propertyInfo.GetValue(item, null);
    headerText = (propValue ?? string.Empty).ToString();
    }

    var tabItem = new TabItem
    {
    DataContext = item,
    Header = headerText,
    Content =
    controlTemplate == null
    ? item
    : new ContentControl { Template = controlTemplate }
    };

    tabItems.Add(tabItem);
    }

    return tabItems;
    }
    return null;
    }
    catch (Exception)
    {
    return null;
    }
    }

    /// <summary>
    /// ConvertBack method is not supported
    /// </summary>
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotSupportedException("ConvertBack method is not supported");
    }

    创建控制模板:
    <ControlTemplate x:Key="MyTabItemContentTemplate">
    <StackPanel>
    <TextBlock Text="{Binding Path=name}" />
    </StackPanel>
    </ControlTemplate>

    并绑定(bind)convert、controltemplate
    <controls:TabControl  x:Name="tabControl"
    ItemsSource="{Binding ElementName=tabControl,
    Path=DataContext,
    Converter={StaticResource ConverterCollectionToTabItems},
    ConverterParameter={StaticResource MyTabItemContentTemplate}}">
    </controls:TabControl>

    取自博客 binding-tabcontrol

    关于silverlight - 将 Silverlight TabControl 绑定(bind)到集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3629888/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com