2011-08-12 3 views
19

현재 Metro 스타일의 윈도우를 구현하려고합니다.
그래서 나는 ResourceDictionary의 내부에 다음과 같은 스타일을했습니다 :WPF ResourceTictionary에서 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> 

내 문제는 내가 얼마나 드 드래그 기능을 구현하는 생각이 없다는 것입니다.
내 dragRectangle에는 Command 속성이 없으므로 MVVM을 사용하여 Rectangle의 MouseLeftButtonDown에서 DragMove()를 호출하려면 어떻게해야합니까? 당신이 이벤트 처리기를 추가하고 전화 DragMove

코드를 설정에서 뒤에 몇 가지 단계가 필요합니다 수 있도록

감사

답변

31

있는 ResourceDictionary는 윈도우 등과 같은 뒤에 코드를 가질 수 있습니다.

  • 당신의 ResourceDictionary에가 호출되면

    MetroStyleResourceDictionary.xaml 당신은 MetroStyleResourceDictionary.xaml.cs
  • 파일 뒤에 코드는 그 후이

    public partial class MetroStyleResourceDictionary 
    { 
        //... 
    } 
    
  • 과 같아야라는 동일한 폴더에 Visual Studio에서 새 파일을 추가하려면 x:Class 특성을 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> 
    

이제 이벤트 핸들러를 MouseLeftButtonDown에 대한 dragRectangle에 추가 할 수 있습니다. 또한 Window의 보류 그렇게 Tag

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

좋은 생각이 될 수 있다는 바인딩을 얻을해야합니다 그리고 마지막으로이

모양을 파일 뒤에 코드에 이벤트 처리기를 추가 할 수 있습니다
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

위대한 !! 게시 해 주셔서 감사합니다 !!! – user500099