如果DataContext更改TabControl没有反应
<controls:TabControl x:Name="tabControlRoom"
ItemsSource="{Binding ElementName=tabControlRoom,
Path=DataContext,
Converter={StaticResource ConverterCollectionToTabItems},
ConverterParameter={StaticResource MyTabItemContentTemplate}}" >
</controls:TabControl>
现在我必须在这里做每次改变
tabControlRoom.ItemsSource = (IEnumerable)new Restaurant.Helpers.Converter.SourceToTabItemsConverter().Convert(
tabControlRoom.DataContext,
null,
this.Resources["MyTabItemContentTemplate"],
null);
СConvert
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
var context = (WorkSpaceContext) value;
ControlTemplate controlTemplate = null;
var date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
if(parameter!=null)
{
try
{
var listParam = (ObservableCollection<Object>) parameter;
if(listParam.Count==3)
{
controlTemplate = (ControlTemplate)listParam[0];
date = (DateTime) listParam[1];
date = date.AddHours(DateTime.Parse(listParam[2].ToString()).Hour);
date = date.AddHours(DateTime.Parse(listParam[2].ToString()).Minute);
}
else if (listParam.Count == 1)
{
}
}
catch (Exception)
{
throw;
}
}
var workPanels = (IEnumerable)context.WorkPanels;
var rooms = context.Rooms;
if (workPanels!= null && rooms != null)
{
var tabItems = new List<TabItem>();
foreach (var room in rooms)
{
int idRoom = room.id;
var nowWorkPanels = from wp in context.WorkPanels
where wp.date == date && wp.Table.id_room == idRoom
select wp;
string headerText = room.name;
if (nowWorkPanels.ToList().Count==0)
{
var tabItem = new TabItem
{
DataContext = room,
Header = headerText,
};
if (controlTemplate == null)
tabItem.Content = (object) room;
else
tabItem.Content = new ContentControl {Template = controlTemplate};
tabItems.Add(tabItem);
}
else
{
var tabItem = new TabItem
{
DataContext = nowWorkPanels.ToList(),
Header = headerText,
};
if (controlTemplate == null)
tabItem.Content = (object) nowWorkPanels.ToList();
else
tabItem.Content = new ContentControl {Template = controlTemplate};
tabItems.Add(tabItem);
}
}
return tabItems;
}
return new List<TabItem>();
}
catch (Exception)
{
return null;
}
}
答案 0 :(得分:0)
你试过这个: -
<controls:TabControl x:Name="tabControlRoom"
ItemsSource="{Binding Converter={StaticResource ConverterCollectionToTabItems},
ConverterParameter={StaticResource MyTabItemContentTemplate}}"
>
</controls:TabControl>
注意当Binding没有指定Path时,分配给当前DataContext的对象将成为使用的值。