简体   繁体   English

XAML中隐藏的数据绑定(不可见)组合框

[英]databinding hidden (non visible) combo box in xaml

I have a problem binding data to a combobox that's in a secondary (not initially focused) tab. 我在将数据绑定到辅助(最初没有重点关注)选项卡中的组合框时遇到问题。 Basically, I have a silverlight form with multiple tabs, whenever I move this combobox to the first (main) tab, the data is loaded and everything works as expected, but when this combo box is in a secondary tab that doesn't have focus initially on first load, the combo box has no data, however, if I data bind the combo box using c# in the code behind it has data! 基本上,我有一个带有多个选项卡的silverlight窗体,每当我将此组合框移动到第一个(主)选项卡时,都会加载数据并且一切都按预期方式工作,但是当此组合框位于没有焦点的第二个选项卡中时最初在第一次加载时,组合框没有数据,但是,如果我使用后面的代码中的c#数据绑定组合框,则它具有数据! Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

My binding in XAML (this doesn't work) 我在XAML中的绑定(这不起作用)

<ComboBox x:Name="MyComboBox"  Height="24" Width="149"
          ItemsSource="{Binding Path=MyList}" DisplayMemberPath="Name"  />

MyList is most likely empty when it's first bound. MyList首次绑定时很可能为空。 Try using an event that is available when you switch to the next tab, to load MyList 尝试使用切换到下一个选项卡时可用的事件来加载MyList

Had the same issue... The solution I found to solve the problem is the following: Whenever your data source (datacontext) changes: iterate through all tab items, select each one, update layout and force rebind. 遇到了相同的问题...我发现要解决该问题的解决方案如下:每当您的数据源(数据上下文)发生更改时:遍历所有选项卡项,选择每个项,更新布局并强制重新绑定。 At the end select back the initial tab item. 最后,选择初始选项卡项。 Something like this (to do in an event handler firing when your data source changes (could be in loaded event in case of initial load): 这样的事情(在数据源更改时在事件处理程序中触发(在初始加载的情况下可能处于加载事件中):

var selectedIndex = tabControl.SelectedIndex;
foreach (TabItem item in tabControl.Items)
{
    tabControl.SelectedItem = item;
    item.UpdateLayout();
    DataContext = null;
    DataContext = ViewModel;  
}
tabControl.SelectedIndex = selectedIndex;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM