2011-08-12 31 views
19

Tôi hiện đang cố gắng triển khai Cửa sổ theo kiểu tàu điện ngầm.
Vì vậy, tôi đã thực hiện các phong cách sau bên trong một ResourceDictionary:Các sự kiện WPF trong ResourceDictionary cho một ControlTemplate

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<!-- Brushes --> 
<SolidColorBrush x:Key="BackgroundColor" Color="#FFFFFFFF" /> 

<!-- Buttons --> 
<Style x:Key="MetroControlBoxButton" TargetType="Button"> 
    <Setter Property="Background" Value="{StaticResource BackgroundColor}" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<!-- Windows --> 
<Style x:Key="MetroWindow" TargetType="Window"> 
    <Setter Property="UseLayoutRounding" Value="True" /> 
    <Setter Property="WindowStyle" Value="None" /> 
    <Setter Property="ResizeMode" Value="NoResize" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Window"> 
       <Grid Background="{StaticResource BackgroundColor}"> 
        <Grid.RowDefinitions> 
         <RowDefinition Height="6" /> 
         <RowDefinition Height="24" /> 
         <RowDefinition Height="*" /> 
         <RowDefinition Height="24" /> 
         <RowDefinition Height="6" /> 
        </Grid.RowDefinitions> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="6" /> 
         <ColumnDefinition Width="*" /> 
         <ColumnDefinition Width="6" /> 
        </Grid.ColumnDefinitions> 

        <Rectangle Name="topLeftBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="0" /> 
        <Rectangle Name="topCenterBorderRectangle" Fill="Orange" Grid.Row="0" Grid.Column="1" /> 
        <Rectangle Name="topRightBorderRectangle" Fill="Red" Grid.Row="0" Grid.Column="2" /> 
        <Rectangle Name="middleLeftBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="0" /> 
        <Rectangle Name="middleRightBorderRectangle" Fill="Orange" Grid.Row="1" Grid.RowSpan="3" Grid.Column="2" /> 
        <Rectangle Name="bottomLeftBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="0" /> 
        <Rectangle Name="bottomCenterBorderRectangle" Fill="Orange" Grid.Row="4" Grid.Column="1" /> 
        <Rectangle Name="bottomRightBorderRectangle" Fill="Red" Grid.Row="4" Grid.Column="2" /> 

        <Rectangle Name="statusBarRectangle" Fill="Yellow" Grid.Row="3" Grid.Column="1" /> 

        <Grid Grid.Row="1" Grid.Column="1"> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*" /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="Auto" /> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="28" /> 
          <ColumnDefinition Width="28" /> 
          <ColumnDefinition Width="28" /> 
         </Grid.ColumnDefinitions> 

         <Rectangle Name="dragRectangle" Fill="Yellow" Grid.Row="0" Grid.Column="1" /> 
         <Button Name="minimizeButton" Content="_" Grid.Row="0" Grid.Column="2" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.MinimizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" /> 
         <Button Name="maximizeButton" Content="[]" Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.MaximizeNormalizeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" /> 
         <Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" Command="{Binding Path=DataContext.CloseCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" /> 
        </Grid> 

        <ContentPresenter Grid.Row="2" Grid.Column="1" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

</ResourceDictionary> 

Vấn đề của tôi là tôi không có ý tưởng làm thế nào để thực hiện tính năng kéo de.
dragRectangle của tôi không có thuộc tính Command vì vậy làm thế nào tôi có thể gọi DragMove() trên MouseLeftButtonDown trên Rectangle bằng MVVM?

Cảm ơn

Trả lời

31

Một ResourceDictionary có thể có mã đằng sau giống như Windows vv để bạn có thể thêm một event handler và gọi DragMove từ đó

Thiết lập mã đằng sau đòi hỏi một vài bước.

  • Nếu ResourceDictionary của bạn được gọi MetroStyleResourceDictionary.xaml bạn thêm một file mới trong Visual Studio trong cùng một thư mục gọi là MetroStyleResourceDictionary.xaml.cs
  • Mã đằng sau tập tin sau đó sẽ giống như thế này

    public partial class MetroStyleResourceDictionary 
    { 
        //... 
    } 
    
  • Sau đó bạn cần thêm thuộc tính x:Class vào tệp Xaml

    <ResourceDictionary x:Class="YourNamespace.MetroStyleResourceDictionary" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
        <!--...--> 
    </ResourceDictionary> 
    

Bây giờ bạn có thể thêm trình xử lý sự kiện vào dragRectangle cho MouseLeftButtonDown. Bạn cũng sẽ cần phải nhận được một tổ chức của các Window để ràng buộc đó để Tag có thể là một ý tưởng tốt

<Rectangle Name="dragRectangle" 
      MouseLeftButtonDown="dragRectangle_MouseLeftButtonDown" 
      Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
      .../> 

Và cuối cùng bạn có thể thêm các xử lý sự kiện vào mã đằng sau tập tin mà sẽ trông như thế này

public partial class MetroStyleResourceDictionary 
{ 
    void dragRectangle_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e) 
    { 
     Rectangle dragRectangle = sender as Rectangle; 
     Window window = dragRectangle.Tag as Window; 
     if (window != null) 
     { 
      window.DragMove(); 
     } 
    } 
} 
+1

Tuyệt vời !! Cảm ơn vì đăng!!! – user500099

Các vấn đề liên quan