觸發器中控制元件依賴屬性繫結失效

LXLR發表於2024-11-21

問題:

該觸發器觸發後,stitchOverTopControl中的IsRotate依賴屬性沒有更新。
<Trigger Property="WarningLevel" Value="2">
   <Setter Property="Content" >
      <Setter.Value>
           <Grid Margin="0,-35,0,0" x:Name="topGrid" VerticalAlignment="Top" >
                <local:StitchOverTopControl HorizontalAlignment="Center" x:Name="stitchOverTopControl" IsRotate="{Binding ElementName=self,Path=IsVaild}">
                </local:StitchOverTopControl>
           </Grid>
      </Setter.Value>
    </Setter>
</Trigger>

原因:

繫結源更新(IsVaild)時,控制元件stitchOverTopControl仍未顯示,則會錯過此次更新。

當控制元件顯示後想要獲取繫結源的最新值,則可在控制元件中增加Load事件,在MyControl_Loaded中獲取最新的繫結源。

<Style TargetType="local:StitchOverTopControl">
            <EventSetter Event="Loaded" Handler="StitchOverTopControl_Loaded"/>
</Style>

private void StitchOverBottomControl_Loaded(object sender, RoutedEventArgs e)
{
    var stitchOverBottomControl = sender as StitchOverBottomControl;
    stitchOverBottomControl.IsRotate = IsVaild;
}

相關文章