2011-08-10 4 views
2

현재 Metro 스타일의 윈도우를 구현하려고합니다.
그래서 나는 ResoruceDictionary 내부에 다음과 같은 스타일을했습니다 :WPF ResourceDictionary에서 Window 템플릿의 이벤트

<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}" /> 
         <Button Name="maximizeButton" Content="[]" Grid.Row="0" Grid.Column="3" Style="{StaticResource MetroControlBoxButton}" /> 
         <Button Name="closeButton" Content="X" Grid.Row="0" Grid.Column="4" Style="{StaticResource MetroControlBoxButton}" /> 
        </Grid> 

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

</ResourceDictionary> 

내 문제는 내가 (최소화, 최대화 및 닫기)를 ControlBox 버튼을 구현하고 싶은 것입니다.
세 개의 버튼을 만들 수 있습니다. 오른쪽 상단에 정렬하십시오. 하지만 내 스타일은 ResourceDictionary 안에 있기 때문에 클릭 이벤트를 구현하는 방법을 모릅니다.

동일한 크기로 조정하고 창을 드래그합니다. 내가 드래그하고 내 창 크기를 조정하는 방법을 알고 있지만 Window_MouseLeftButtonDown 이벤트에 연결할 필요가 있지만 템플릿 내에서 연결하는 방법을 모른다.

나는 내가 클래스를 만들 "수"와 여기에 내 코드를 넣어 보았다
Is it possible to set code behind a resource dictionary in WPF for event handling?
을하지만 좋은 방법이 아니다 보인다.

저는 MetroWindow를 만들고 다른 Windows에서 상속받을 수 있다고 생각했습니다. 그러나 WPF는 Visual Inheritance를 지원하지 않습니다.

그래서 가장 좋은 방법은 내 응용 프로그램을 통해 두 개 이상의 창에서 내 창 템플릿을 재사용 할 수 있습니다.

감사합니다,
SiriusNik

답변

1

나는 버튼 문제로 조금 혼란 스러워요. 스타일의 Template 속성에 버튼을 만들었지 만, 그 스타일을 내가보기에는 보이지 않는 버튼 객체에 적용해야합니다. click 이벤트 또는 명령이 button 요소에 추가됩니다.

예를 들어

:

<Button x:Name="testButton" Style="{StaticResource MetroControlBoxButton}" ... /> 

UPDATE :

Command="{Binding Path=DataContext.ClickCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" 
:

가 컨트롤 템플릿에서 윈도우의 DataContext에의 명령에 결합하려면, 당신은 버튼 정의에이 요소를 추가해야 할 수 있습니다

+0

원래 게시물을 업데이트하여 버튼을 추가했습니다. 그렇다면 CloseButton은 어떻게 Close()를 실행할 수 있습니까? 템플릿 내부의 메소드? – SiriusNik

+0

템플릿에 있어도 단추를 정의 했으므로 명령 바인딩을 수행하여 Window의 DataContext에있는 작업을 수행 할 수 있습니다. 이것이 ResourceDictionary에 정의 된 경우 이벤트를 찾을 수 없기 때문에 이것이 수행 할 수있는 유일한 방법이라고 생각합니다. 내가 틀렸다면, 누군가 제발 고쳐주세요. 예제를 보여주기 위해 아래 답변을 업데이트했습니다. – Josh

+0

당신의 대답에서 나는 약간의 지식이 빠져 있음을 깨달았습니다. 몇 가지 조사를 한 후에 MVVM에 대해 아무 것도 몰랐습니다. 그래서 MetroWindowViewModel을 만들었고 명령을 성공적으로 바인딩 할 수있었습니다. 그러나 하나의 질문, 여러 Windows에서 내 Window 템플릿을 사용하려면 각각의 새 Windows ModelView가 MetroWindowModelView를 상속해야합니다. – SiriusNik

관련 문제