2011-11-11 75 views

回答

1

This converter 爲我工作。

DialogPresenter.Generic.xaml

<controls:DragCanvas Name="DialogCanvas"> 
    <Grid x:Name="Dialog" ...> 
    <Canvas.Top> 
     <MultiBinding Converter="{StaticResource CenterConverter}" ConverterParameter="top"> 
      <Binding ElementName="DialogCanvas" Path="ActualWidth" /> 
      <Binding ElementName="DialogCanvas" Path="ActualHeight" /> 
      <Binding ElementName="Dialog" Path="ActualWidth" /> 
      <Binding ElementName="Dialog" Path="ActualHeight" /> 
     </MultiBinding> 
    </Canvas.Top> 
    <Canvas.Left> 
     ... 
    </Canvas.Left> 
0

有幾種方法可以讓對話框居中。

  1. 在DialogPresenter類,更新顯示()方法來設置window.WindowStartupPosition = WindowStartupLocation.CenterScreen。

  2. 編寫自定義TriggerAction並覆蓋Invoke以執行以下操作。

    var window = Window.GetWindow(this.AssociatedObject); window.WindowStartupLocation.CenterScreen

    然後將觸發器操作附加到觸發器部分中的ControlTemplate。你可以使用一個使用LoadedEvent的EventTrigger並調用你的TriggerAction。

注意:如果您有ExpressionBlend,它會將TriggerActions拖放到UI變得很容易,因爲它會爲您編寫EventTrigger部件。