2012-05-04 63 views
1

我已經創建了一個UserControl名稱TitleBar,它放置在每個視圖中。從UserControl按鈕關閉Caliburn Micro View?

TitleBar.xaml包含一個Button來關閉它包含的窗口。

如何關閉使用該按鈕的Caliburn窗口。

TitleBar中用戶控件

<UserControl x:Class="JIMS.Controls.TitleBar" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"> 
    <Grid Style="{StaticResource TitleBar}"> 
     <Rectangle HorizontalAlignment="Stretch" Height="7" Margin="0,0,-5,0" VerticalAlignment="Top" Fill="{DynamicResource DefaultBrush}"></Rectangle> 
     <Grid HorizontalAlignment="Left" Margin="-10,-5,0,0" Name="Logo"> 
      <TextBlock Name="txtTitle" Style="{StaticResource Title}">JIMS</TextBlock>    
      <Ellipse HorizontalAlignment="Right" Margin="0,0,5,0" Width="20" Height="20"> 
       <Ellipse.Fill> 
        <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_settings_white}" /> 
       </Ellipse.Fill> 
      </Ellipse> 
     </Grid> 
     <Grid HorizontalAlignment="Right"> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,5,-4,0"> 
       <Button Name="btnClose" Style="{StaticResource ChromeButtonStyle}" Click="btnClose_Click" IsTabStop="False"> 
        <TextBlock TextWrapping="Wrap" Text="r" FontFamily="Webdings" Foreground="#FF919191" FontSize="13.333" /> 
       </Button> 
      </StackPanel> 
     </Grid> 
    </Grid> 
</UserControl> 

在查看

<UserControl xmlns:my="clr-namespace:JIMS.Controls;assembly=JIMS.Controls" x:Class="JIMS.Views.Stock.UnitView" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
     Name="Unit">   
    <Border Style="{StaticResource WindowBorderStyle}"> 
     <StackPanel Orientation="Vertical"> 
      <my:TitleBar Title="unit creation"/> 
      <StackPanel Visibility="{Binding ControlVisiblity}" Orientation="Horizontal" Margin="0,5,0,5"> 
       <StackPanel Orientation="Vertical" Margin="10,0,0,0"> 
        <Label>Short Name :</Label> 
        <Label>Unit Name :</Label>      
       </StackPanel> 
       <StackPanel Orientation="Vertical" Width="200" Margin="0,0,10,0"> 
        <TextBox Name="txtShortName" Text="{Binding Path=UnitShort}"></TextBox> 
        <TextBox Name="txtUnitName" Text="{Binding Path=UnitName}"></TextBox>      
       </StackPanel> 
      </StackPanel> 
      <Expander Style="{StaticResource DisplayExpander}" IsExpanded="{Binding IsDisplayExpanded}" Header="display units"> 
       <StackPanel Orientation="Horizontal" Margin="0,5,0,5" Visibility="{Binding DisplayVisiblity}"> 
        <DataGrid AutoGenerateColumns="True" Height="200" MinWidth="300" ItemsSource="{Binding Display}"></DataGrid> 
       </StackPanel> 
      </Expander> 
      <StackPanel Orientation="Horizontal" HorizontalAlignment="Right"> 
       <Button Name="SaveUnit" Style="{StaticResource MetroButton}">Save</Button> 
      </StackPanel> 
     </StackPanel> 
    </Border> 
</UserControl> 
+0

是否命名的按鈕TryClose工作?因此,請將btnClose改爲TryClose。 –

回答

1

在您的TitleBar中控制TitleBar中使用定義RoutedEvent這樣

 public event RoutedEventHandler CloseClick 
    { 
     add { AddHandler(CloseClickEvent, value); } 
     remove { RemoveHandler(CloseClickEvent, value); } 
    } 

    public static readonly RoutedEvent CloseClickEvent = EventManager.RegisterRoutedEvent(
     "CloseClick", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(TitleBar)); 

    void RaiseCloseClickEvent() 
    { 
     var newEventArgs = new RoutedEventArgs(TitleBar.CloseClickEvent); 
     RaiseEvent(newEventArgs); 
    } 

    private void btnClose_Click(object sender, RoutedEventArgs e) 
    { 
     RaiseCloseClickEvent(); 
    } 

,並附加btnClose_Click事件處理程序btnClose控制在TitleBar

現在,當您使用TitleBar添加這樣

 <my:TitleBar Title="This is the title"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="CloseClick"> 
        <cal:ActionMessage MethodName="Close"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </my:TitleBar> 

動作這將調用方法Close您的視圖模型時在TitleBar上提出CloseClickEvent

對於關閉窗口,你可以從Screen獲得您的視圖模型,並添加以下代碼片段

public void Close() 
{ 
    TryClose(); 
} 
+0

它的工作..但我不想在每個視圖中寫這麼多的代碼。這可以通過TitleBar.xaml來實現 –