如果 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);
С转换
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;
}
}
你试过这个吗:-
<controls:TabControl x:Name="tabControlRoom"
ItemsSource="{Binding Converter={StaticResource ConverterCollectionToTabItems},
ConverterParameter={StaticResource MyTabItemContentTemplate}}"
>
</controls:TabControl>
请注意,当绑定(bind)没有指定路径时,分配给当前 DataContext 的对象将成为使用的值。
我是一名优秀的程序员,十分优秀!