从UserControl按钮关闭Caliburn Micro View?

时间:2012-05-04 00:29:07

标签: wpf view user-controls caliburn.micro

我创建了一个UserControl名称TitleBar,它放在每个视图中。

TitleBar.xaml包含一个用于关闭其中包含的窗口的按钮。

如何使用该按钮关闭Caliburn窗口。

TitleBar UserControl

<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>

视图中的TitleBar用法

<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>

1 个答案:

答案 0 :(得分:1)

在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上出现CloseClickEvent时,这会在您的viewmodel上调用方法TitleBar

要关闭窗口,您可以从Screen派生您的viewmodel并添加以下代码段

public void Close()
{
    TryClose();
}