我有一个UserControl
包含的TabControl
。
<UserControl x:Class="Test.MyUC"
....
xmlns:vm="clr-namespace:Test.ViewModels"
xmlns:ikriv="clr-namespace:IKriv.Windows.Controls.Behaviors"
...
<UserControl.Resources>
<vm:MyUCVM x:Key="VM" />
</UserControl.Resources>
<UserControl.DataContext>
<StaticResourceExtension ResourceKey="VM" />
</UserControl.DataContext>
<!-- Using Ivan Krivyakov's Attached Behavior -->
<TabControl ikriv:TabContent.IsCached="True"
TabStripPlacement="Top" ItemsSource="{Binding TabList}" IsSynchronizedWithCurrentItem="True">
<TabControl.Resources>
<DataTemplate DataType="{x:Type vm:MyTab1VM}">
<v:MyTab1/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MyTab2VM}">
<v:MyTab2/>
</DataTemplate>
</TabControl.Resources>
...
当然,在MyUCVM
,我有TabList
。现在,到目前为止,一切正常。
当TabControl
需要从某个外部源(ViewModel
当然是完成)连续递归地读取数据,并将这些数据传递给View
(通过Binding
)进行显示时,问题就开始出现了(例如MyTab1)。到目前为止,一切正常。但是,我不希望该选项卡不可见时运行,因为这样做是没有意义的。
为此,MyTab1VM
需要知道关联的视图(MyTab1
)是否为所选选项卡。因此,我将其连接起来:
MyTab1:
<Style TargetType="TabItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWayToSource}" />
</Style>
MyTab1VM
public static readonly DependencyProperty IsSelectedProperty =
DependencyProperty.Register("IsSelected",
typeof(bool),
typeof(MyTab1VM),
new PropertyMetadata(false, new PropertyChangedCallback(IsSelectedChanged))
);
public bool IsSelected
{
get
{
return (bool) GetValue(IsSelectedProperty);
}
set
{
SetValue(IsSelectedProperty, value);
}
}
public static void IsSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.Property == IsSelectedProperty)
{
MyTab1VM vm = d as MyTab1VM ;
vm.SetupToGetData();
}
}
private void SetupToGetData()
{
if (this.IsSelected)
{
System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(100);
timer.Tick += timer_Tick;
timer.Start();
}
}
private void timer_Tick(object sender, EventArgs e)
{
if (this.IsSelected)
this.MyData = ExternalSource.GetData();
else
{
(sender as System.Windows.Threading.DispatcherTimer).Stop();
}
}
不幸的是,只有当我this.IsSelected = true;
在MyTab1VM
的构造函数中进行手动设置时,此设置才有效。将其保留在构造函数中,数据不会显示在视图中。
我设置了断点并确认绑定IsSelected
正确运行。甚至计时器正在运行,并且ExternalSource.GetData()
正在被调用。但是this.MyData = ExternalSource.GetData();
不会触发从ViewModel到View的更改。
最令人困惑的是,如果从构造函数IsSelected
设置为true
,则会触发相同的绑定。
外面有人知道这里发生了什么吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句