我有一个标签控件,有两个标签。两个选项卡都有一个gridview,并由BackgroundWorker进程加载以加载数据并设置selectedItem属性。加载时可见的第一个选项卡会选择所选项目。当我切换到第二个选项卡时,即使正确设置了支持属性,它也不会显示gridview的selecteditem。如果我绕过BackgroundWorker并在UserControl_Loaded上加载datacontext,则在两个选项卡上都会显示选定项。
此外,如果我交换选项卡,第一个选项卡始终可以正常工作。
有人知道在通过backgroundworker设置datacontext时正确显示选择项的解决方法吗?
WrapperPage.xaml ----
<UserControl x:Class="CCR.CCR"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:page="clr-namespace:CCR.Pages"
Loaded="UserControl_Loaded"
>
<!-- MAIN TABS -->
<TabControl
Grid.Row="1"
Margin="0 8 0 0"
>
<TabItem Header="First List">
<page:FirstList/>
</TabItem>
<TabItem Header="Notes" IsEnabled="{Binding Notes_Enabled}">
<page:NoteList />
</TabItem>
</TabControl>
NoteList.xaml ----
<telerikGrid:RadGridView x:Name="NoteGrid"
ItemsSource="{Binding NoteList}"
SelectedItem="{Binding SelectedItem}"
IsSynchronizedWithCurrentItem="False"
>
NoteList_MV
public class NoteList_MV : INotifyPropertyChanging, INotifyPropertyChanged
{
#region Constants and Variables
BASENOTE_MV m_ModelView;
protected ObservableCollection<SVNOTE_MVRec> m_NoteList;
protected SVNOTE_MVRec m_SelectedItem;
#endregion
#region Properties
public ObservableCollection<SVNOTE_MVRec> NoteList
{
get { return m_NoteList; }
set
{
OnPropertyChanging("NoteList");
m_NoteList = value;
OnPropertyChanged("NoteList");
}
}
public SVNOTE_MVRec SelectedItem
{
get { return m_SelectedItem; }
set
{
OnPropertyChanging("SelectedItem");
m_SelectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
#endregion
public NoteList_MV()
{
short bStat = 0;
m_NoteList = new ObservableCollection<SVNOTE_MVRec>();
m_ModelView = new SVNOTE_MV();
m_ModelView.Open();
if (m_ModelView.IsOpened)
{
string Key = sKey("SVNOTE");
SVNOTE_MVRec tempRec = m_ModelView.LoadRec(Key, (ushort)BTROPS.B_GET_LTE, ref bStat);
while (bStat == 0 && tempRec.AR_NUM == Key.Substring(0, 15))
{
AddRec(tempRec);
tempRec = m_ModelView.LoadRec(Key, (ushort)BTROPS.B_GET_PREVIOUS, ref bStat);
}
m_ModelView.Close();
}
}
private void AddRec(SVNOTE_MVRec tempRec)
{
if (tempRec.REC_TYPE.Substring(0, 2) == "N1")
{
m_NoteList.Add(tempRec);
if (m_SelectedItem == null || m_SelectedItem.DISPLAY_DATE.Value < tempRec.DISPLAY_DATE.Value)
m_SelectedItem = tempRec;
}
}
}
NoteList.xaml.cs
namespace CCR.Pages
{
public partial class NoteList : BaseUserControl
{
#region Constants and Variables
protected NoteList_MV m_ModelView;
#endregion
#region Methods
public NoteList()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(NoteList_Loaded);
}
#endregion
#region Events
void NoteList_Loaded(object sender, RoutedEventArgs e)
{
if (m_ModelView == null) wrker.RunWorkerAsync();
}
protected override void wrker_DoWork(object sender, DoWorkEventArgs e)
{
m_ModelView = new NoteList_MV();
}
protected override void wrker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataContext = m_ModelView;
NoteGrid.IsBusy = false;
}
}