gpt4 book ai didi

c# - 如何更新 ItemSsource?

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:26 24 4
gpt4 key购买 nike

如果 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 的对象将成为使用的值。

关于c# - 如何更新 ItemSsource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6276696/

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