Aplicación Silverlight 3 con un TabControl vinculado a un ObservableCollection utilizando un IValueConverter. Inicialmente funciona el enlace (convertidor llamado) al inicio de la aplicación. Los cambios, Borrar() o Agregar() a la colección encuadernada no se reflejan en el convertidor de TabControl ... no llamado.Silverlight TabControl vinculado a ObservableCollection <string> no se actualiza cuando se cambió la colección
nota: el ListBox enlazado refleja los cambios en la colección enlazada mientras que el TabControl no lo hace.
Ideas?
/JHD
El XAML unión ...
<UserControl.Resources>
<local:ViewModel x:Key="TheViewModel"/>
<local:TabConverter x:Key="TabConverter" />
</UserControl.Resources>
<StackPanel DataContext="{StaticResource TheViewModel}">
<ListBox ItemsSource="{Binding Classnames}" />
<controls:TabControl x:Name="TheTabControl"
ItemsSource="{Binding Classnames, Converter={StaticResource TabConverter}, ConverterParameter=SomeParameter}"/>
<Button Click="Button_Click" Content="Change ObservableCollection" />
</StackPanel>
El modelo de vista ...
namespace DatabindingSpike
{
public class ViewModel
{
private ObservableCollection<string> _classnames = new ObservableCollection<string>();
public ViewModel()
{
_classnames.Add("default 1 of 2");
_classnames.Add("default 2 of 2");
}
public ObservableCollection<string> Classnames
{
get { return _classnames; }
set { _classnames = value; }
}
}
}
El convertidor (por completo) ...
namespace DatabindingSpike
{
public class TabConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var source = value as ObservableCollection<string>;
if (source == null)
return null;
var param = parameter as string;
if (string.IsNullOrEmpty(param) || param != "SomeParameter")
throw new NotImplementedException("Null or unknow parameter pasased to the tab converter");
var tabItems = new List<TabItem>();
foreach (string classname in source)
{
var tabItem = new TabItem
{
Header = classname,
Content = new Button {Content = classname}
};
tabItems.Add(tabItem);
}
return tabItems;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Cualquier posibilidad de que el modo por defecto es un solo uso? Intentará configurar el modo explícitamente. /jhd –
Probado explícitamente Modo = OneWay, no joy. Usaré el evento CollectionChanged y restableceré el TabControl.ItemsSource hasta que encuentre una mejor manera./jhd –
He creado el control de pestañas extendido que funciona correctamente con la clase ObservableCollection. http://vortexwolf.wordpress.com/2011/04/09/silverlight-tabcontrol-with-data-binding/ – vorrtex