gpt4 book ai didi

银光 4 : Making Closeable Tabitems

转载 作者:行者123 更新时间:2023-12-04 03:11:02 26 4
gpt4 key购买 nike

我想扩展选项卡控件以具有可关闭的选项卡项目。

我找到了 Kent 的这个 WPF 解决方案:
On the WPF TabControl - can I add content next to the tab headers?

我在 Blend 中打开了现有 Silverlight tabcontrol 的副本。然而,结构看起来与 WPF 选项卡控件完全不同。我无法将其直接放入 Silverlight 控件模板中。

有人知道我的好资源吗?

最佳答案

我之前也遇到过同样的问题,然后我决定使用扩展 TabControl .我不知道在哪里找到它,但没关系,现在它在我的项目中。

有了这个 TabControl我可以在 ViewModel 集合中添加或删除项目,我的更改将反射(reflect)在用户界面上。

MyTabControl.cs

public class MyTabControl : TabControl
{
public MyTabControl()
: base()
{
this.SelectionChanged += OnSelectionChanged;
}

#region Tabs with databinding and templates
/// <summary>
/// Template for a TabItem header
/// </summary>
public DataTemplate TabHeaderItemTemplate
{
get { return (DataTemplate)GetValue(TabHeaderItemTemplateProperty); }
set { SetValue(TabHeaderItemTemplateProperty, value); }
}
public static readonly DependencyProperty TabHeaderItemTemplateProperty =
DependencyProperty.Register("TabHeaderItemTemplate", typeof(DataTemplate), typeof(MyTabControl), new PropertyMetadata(
(sender, e) =>
{
((MyTabControl)sender).InitTabs();
}));

/// <summary>
/// Template for a content
/// </summary>
public DataTemplate TabItemTemplate
{
get { return (DataTemplate)GetValue(TabItemTemplateProperty); }
set { SetValue(TabItemTemplateProperty, value); }
}
public static readonly DependencyProperty TabItemTemplateProperty =
DependencyProperty.Register("TabItemTemplate", typeof(DataTemplate), typeof(MyTabControl), new PropertyMetadata(
(sender, e) =>
{
((MyTabControl)sender).InitTabs();
}));

/// <summary>
/// Source of clr-objects
/// </summary>
public IEnumerable MyItemsSource
{
get
{
return (IEnumerable)GetValue(MyItemsSourceProperty);
}
set
{
SetValue(MyItemsSourceProperty, value);
}
}

public static readonly DependencyProperty MyItemsSourceProperty =
DependencyProperty.Register("MyItemsSource", typeof(IEnumerable), typeof(MyTabControl), new PropertyMetadata(
(sender, e) =>
{
MyTabControl control = (MyTabControl)sender;
INotifyCollectionChanged incc = e.OldValue as INotifyCollectionChanged;
if (incc != null)
{
incc.CollectionChanged -= control.MyItemsSourceCollectionChanged;
}
control.InitTabs();

incc = e.NewValue as INotifyCollectionChanged;
if (incc != null)
{
incc.CollectionChanged += control.MyItemsSourceCollectionChanged;
}
}));


/// <summary>
/// Selected item as object
/// </summary>
public object MySelectedItem
{
get { return (object)GetValue(MySelectedItemProperty); }
set { SetValue(MySelectedItemProperty, value); }
}

// Using a DependencyProperty as the backing store for MySelectedItem. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MySelectedItemProperty =
DependencyProperty.Register("MySelectedItem", typeof(object), typeof(MyTabControl), new PropertyMetadata(
(sender, e) =>
{
MyTabControl control = (MyTabControl)sender;

if (e.NewValue == null)
control.SelectedItem = null;
else
{
var tab = control.Items.Cast<TabItem>().FirstOrDefault(ti => ti.DataContext == e.NewValue);
if (tab != null && control.SelectedItem != tab)
control.SelectedItem = tab;
}
}));

private void InitTabs()
{
Items.Clear();
if (MyItemsSource != null && MyItemsSource.OfType<object>().Any())
{
int i = 0;
foreach (var item in MyItemsSource)
{
var newitem = new TabItem();

if (TabItemTemplate != null)
newitem.Content = TabItemTemplate.LoadContent();

if (TabHeaderItemTemplate != null)
newitem.Header = TabHeaderItemTemplate.LoadContent();

newitem.DataContext = item;
Items.Add(newitem);
}
VisualStateManager.GoToState(this, "Normal", true);
}
else VisualStateManager.GoToState(this, "NoTabs", true);
}

private void MyItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewStartingIndex > -1)
{
foreach (var item in e.NewItems)
{
var newitem = new TabItem();

if (TabItemTemplate != null)
newitem.Content = TabItemTemplate.LoadContent();

if (TabHeaderItemTemplate != null)
newitem.Header = TabHeaderItemTemplate.LoadContent();

newitem.DataContext = item;
Items.Add(newitem);
}
}
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove)
{
if (e.OldStartingIndex > -1)
{
var ti = (TabItem)this.Items[e.OldStartingIndex];
Items.RemoveAt(e.OldStartingIndex);
}
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Replace)
{
Items.RemoveAt(e.OldStartingIndex);

var newitem = new TabItem();

if (TabItemTemplate != null)
newitem.Content = TabItemTemplate.LoadContent();

if (TabHeaderItemTemplate != null)
newitem.Header = TabHeaderItemTemplate.LoadContent();

newitem.DataContext = e.NewItems[0];

Items.Add(newitem);
Items.Insert(e.NewStartingIndex, newitem);
}
else if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Reset)
{
InitTabs();
}
}

#endregion

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var si = e.AddedItems.Cast<TabItem>().FirstOrDefault();
if (si != null)
this.MySelectedItem = si.DataContext;
else this.MySelectedItem = null;
}
}

MainPage.xaml
<my:MyTabControl MyItemsSource="{Binding Items}" MySelectedItem="{Binding SelectedITem}">
<my:MyTabControl.TabHeaderItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Title}" VerticalAlignment="Center"/>
<Button Content="X" Margin="3" Width="20" Height="20" Grid.Column="1"
Command="{Binding RequestCloseCommand}"/>
</Grid>
</DataTemplate>
</my:MyTabControl.TabHeaderItemTemplate>
<my:MyTabControl.TabItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding Content}"/>
</DataTemplate>
</my:MyTabControl.TabItemTemplate>
</my:MyTabControl>

请注意,这些属性被称为 MyItemsSourceMySelectedItem , 因为这个 TabControl使用对象,而不是 TabItem .

和两个 ViewModel:
MainViewModel.cs
public class MainViewModel
{
public MainViewModel()
{
this.Items = new ObservableCollection<TabItemViewModel>
{
new TabItemViewModel("Tab 1", OnItemRequestClose),
new TabItemViewModel("Tab item 2", OnItemRequestClose)
};
}

public ObservableCollection<TabItemViewModel> Items { get; set; }

public void OnItemRequestClose(TabItemViewModel item)
{
this.Items.Remove(item);
}
}

TabItemViewModel.cs
public class TabItemViewModel
{
public TabItemViewModel(string title, Action<TabItemViewModel> onClose)
{
this.Title = title;
this.RequestCloseCommand = new DelegateCommand(_ => onClose(this));

//Just a demontration
this.Content = "Test content "+title;
}

public string Title { get; set; }

public ICommand RequestCloseCommand { get; set; }

public object Content { get; set; }
}

关于银光 4 : Making Closeable Tabitems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4828661/

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