我对 tabcontrol 控制有疑问。
我正在使用绑定和转换器将项目动态添加到 tabcontrol。
添加第二个 tabitem 时出现以下异常(参见下面的代码):值不在预期范围内
它的堆栈跟踪:
- MS.Internal.XcpImports.CheckHResult(UInt32 hr) - MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty 属性, DependencyObject doh) - MS.Internal.XcpImports.SetValue(IManagedPeerBase doh, DependencyProperty 属性, Object obj) - System .Windows.DependencyObject.SetObjectValueToCore(DependencyProperty dp, Object value) в System.Windows.DependencyObject.SetEffectiveValue(DependencyProperty property, EffectiveValueEntry& newEntry, Object newValue) в System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation操作)? System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp,对象值,布尔allowReadOnlySet)? System.Windows.Controls.ContentControl。set_Content(Object value) × SilverlightApplication1.Services.TabConverter.Convert(Object value, Type targetType, Object parameter, CultureInfoculture)
主页 xaml:
<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="1024"
d:DesignWidth="1280"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
xmlns:local="clr-namespace:SilverlightApplication1.Services">
<UserControl.Resources>
<local:TabConverter x:Key="tabConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="4*" />
</Grid.ColumnDefinitions>
<ListBox SelectionChanged="ListBox_SelectionChanged">
<ListBoxItem Content="ViewA"></ListBoxItem>
<ListBoxItem Content="ViewB"></ListBoxItem>
</ListBox>
<sdk:TabControl Grid.Column="1"
ItemsSource="{Binding Tabs, Converter={StaticResource tabConverter}}" />
</Grid>
MainPage 代码隐藏:
public partial class MainPage : UserControl {
ViewModel viewModel = new ViewModel();
public MainPage() {
InitializeComponent();
this.DataContext = viewModel;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
try {
viewModel.Tabs.Add(new TabItemModel() {
Header = "New Tab",
Content = new Grid()
});
// sdk:tabcontrol does not listen CollectionChanged of viewModel.Tabs.
// thats why:
viewModel.Tabs = viewModel.Tabs;
} catch (Exception) { }
}
}
数据模型:
public class TabItemModel {
public string Header { get; set; }
public UIElement Content { get; set; }
}
查看型号:
public class ViewModel:INotifyPropertyChanged {
ObservableCollection<TabItemModel> tabs = new ObservableCollection<TabItemModel>();
public ObservableCollection<TabItemModel> Tabs {
get { return tabs; }
set { tabs = value; OnPropertyChanged(PropertyNames.Tabs); }
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string property) {
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
class PropertyNames {
public const string Tabs = "Tabs";
}
}
标签转换器:
public class TabConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
try {
var object_data = value as ObservableCollection<TabItemModel>;
var result = new List<TabItem>();
foreach (var item in object_data) {
result.Add(new TabItem() {
Header = item.Header,
Content = item.Content // if comment this, everything works
});
}
return result;
} catch (Exception e) {
MessageBox.Show(e.StackTrace, e.Message, MessageBoxButton.OK);
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
throw new NotImplementedException();
}
}