1

我创建了一个名为 TitleBar 的 UserControl,它被放置在每个视图中。

TitleBar.xaml 包含一个按钮来关闭它包含的窗口。

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

标题栏用户控件

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

1 回答 1

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_TitleBar

要关闭窗口,您可以从中派生视图模型Screen并添加以下代码段

public void Close()
{
    TryClose();
}
于 2012-05-04T07:20:52.593 回答