我有一个主UI容器(让我们说Canvas)具有UI切换功能:
FlipControl
Panorama
控件两个SelectedItem属性都绑定到我的主UI ViewModel中的CurrentItem属性,但是当从FlipControl切换到Panorama(或反之亦然)时,属性首先使用正确的选定值更新,并在第二次使用null更新(我想由于一些可见性改变逻辑)。
有没有办法避免接口交换机上的null SelectedItem?
代码:
<controls:ZoomBox>
<controls:ZoomBox.Levels>
<controls:FlipControl Grid.Column="1" Items="{Binding FilteredCards,UpdateSourceTrigger=PropertyChanged}" IsManipulationEnabled="True" LabelVisibility="Collapsed"
SelectedElement="{Binding CurrentCard,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"/>
<controls:Panorama Items="{Binding FilteredCards,UpdateSourceTrigger=PropertyChanged}" Height="400"
SelectedElement="{Binding CurrentCard,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" />
</controls:ZoomBox.Levels>
</controls:ZoomBox>
Panorama SelectedElement依赖属性:
public static readonly DependencyProperty SelectedElementProperty =
DependencyProperty.Register("SelectedElement",typeof(Card),typeof(Panorama), new PropertyMetadata(default(Card)));
public Card SelectedElement
{
get { return (Card)GetValue(SelectedElementProperty); }
set { SetValue(SelectedElementProperty, value); }
}
FlipControl SelectedElement属性
public static readonly DependencyProperty SelectedElementProperty =
DependencyProperty.Register("SelectedElement",typeof(Card),typeof(FlipControl),
new PropertyMetadata(default(Card)));
public Card SelectedElement
{
get { return (Card)GetValue(SelectedElementProperty); }
set { SetValue(SelectedElementProperty, value); }
}
和Binding(两个控件都相同)
<ListView x:Name="itemsControl"
SelectedItem="{Binding ElementName=mainControl,Path=SelectedElement,Mode=TwoWay}"
ItemsSource="{Binding ElementName=mainControl,Path=Items}" />
ZoomBox切换控件之间的当前级别更改(使用在启动时初始化一次的相同实例)。