2013-06-30 236 views
3

我是WPF中的新成員,我需要製作一個按鈕(X)來模擬表格中的X按鈕。首先我設置WindowStyle="None"。然後做一個Rectangle在WPF中添加X關閉按鈕

<Rectangle Height="16" HorizontalAlignment="Left" Margin="482,4,0,0" Name="x_btn" Stroke="#00000000" VerticalAlignment="Top" Width="17" MouseEnter="x_btn_MouseEnter" MouseLeftButtonUp="x_btn_MouseLeftButtonUp"> 
    <Rectangle.Fill> 
     <ImageBrush ImageSource="/Red%20Crescent;component/Images/x_btn.png" /> 
    </Rectangle.Fill>    
</Rectangle> 

這個我想改變後面的代碼Background Image OnMousEnter事件之後:

private void x_btn_MouseEnter(object sender, MouseEventArgs e) 
{   
} 

private void x_btn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) 
{ 
    this.Close(); 
} 

請幫助一樣快,您可以。

注:我試圖使用Button,但它留下了一個邊框,我不想這樣。

回答

6

在你的情況下,最好使用Style作爲Button,並使用Path而不是Image。要正確實現關閉功能,最好通過DependencyProperty來實現,並直接在Style中設置該值。

<Window x:Class="CloseButtonHelp.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:CloseButtonHelp" 
     Title="MainWindow" Height="350" Width="525"> 

<Window.Resources> 
    <!-- Our style for the ToggleButton --> 
    <Style x:Key="ToggleButtonWindowClose" TargetType="{x:Type ToggleButton}"> 
     <!-- Here you can set the initial properties for the control --> 
     <Setter Property="Background" Value="Transparent" /> 
     <Setter Property="SnapsToDevicePixels" Value="True" /> 

     <!-- Template needs to completely re-writing the standard control --> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToggleButton}"> 
        <Grid> 
         <!-- Then responsible for the content. In our case it did not really need, because it is set Path --> 
         <ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />  
         <!-- Our Path. Shows a cross -->      
         <Path x:Name="CloseWindow" SnapsToDevicePixels="True" ToolTip="Close window" Width="18" Height="17" Margin="0,0,10,0" HorizontalAlignment="Right" VerticalAlignment="Center" Stretch="Fill" Fill="#2D2D2D" Data="F1 M 26.9166,22.1667L 37.9999,33.25L 49.0832,22.1668L 53.8332,26.9168L 42.7499,38L 53.8332,49.0834L 49.0833,53.8334L 37.9999,42.75L 26.9166,53.8334L 22.1666,49.0833L 33.25,38L 22.1667,26.9167L 26.9166,22.1667 Z " /> 
        </Grid> 

        <!-- Trigger fires on the property --> 
        <ControlTemplate.Triggers> 
         <!-- Here change the color when the mouse cursor --> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter TargetName="CloseWindow" Property="Fill" Value="#C10000" /> 
         </Trigger> 

         <!-- Use ToggleButton, because it has a property IsChecked, accessible through the style --> 
         <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="local:WindowBehaviours.Close" Value="True" /> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</Window.Resources> 

<Grid> 
    <!-- Set the our style by key ---> 
    <ToggleButton Name="CloseButton" Style="{StaticResource ToggleButtonWindowClose}" /> 
</Grid> 
</Window> 

WindowBehaviorsClass上市:

public static class WindowBehaviours 
{ 
    // Closing window 
    public static void SetClose(DependencyObject target, bool value) 
    { 
     target.SetValue(CloseProperty, value); 
    } 

    public static readonly DependencyProperty CloseProperty = 
               DependencyProperty.RegisterAttached("Close", 
               typeof(bool), 
               typeof(WindowBehaviours), 
               new UIPropertyMetadata(false, OnClose)); 

    private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
    { 
     if (e.NewValue is bool && ((bool)e.NewValue)) 
     { 
      Window window = GetWindow(sender); 

      if (window != null) 
      { 
       window.Close(); 
      } 
     } 
    } 

    private static Window GetWindow(DependencyObject sender) 
    { 
     Window window = null; 

     if (sender is Window) 
     { 
      window = (Window)sender; 
     } 

     if (window == null) 
     { 
      window = Window.GetWindow(sender); 
     } 

     return window; 
    } 
} 

更多信息參見MSDN

+0

感謝您的代碼..但是當您使用模板時,您能描述xaml代碼...因爲我是wpf的新手 –

+0

請參閱我的編輯。爲了更好地理解風格,研究相關資源(MSDN,WPF圖書),因爲要解釋所有的答案都不是真實的。 –

+0

謝謝你的描述......不過我有一個問題,如果你不介意的話...我怎樣才能建立一個分離式的文件,只是添加到表單 ,因爲我有一個大的計劃和控制很多 –