2014-06-07 2 views
0

먼저 내 영어를 사과합니다.WPF combie 3 객체를 영역 버튼에 넣습니다.

저는 간단한 C# WPF 앱을 만들고 있습니다. 내 MainWindow.xaml에 타원 label1, label2를 추가했습니다.

이 3 개의 객체 (타원, label1, label2)를 버튼으로 만들고 싶습니다. 이 버튼이 "Size"라는 두 번째 wpf 페이지를로드하기를 원합니다. 내가 이해하는 한 그것은 지역 버튼이어야합니다. WPF 컨트롤에서 "border"를 "MouseDown"(이벤트 핸들러에서)과 함께 사용하려고했지만 효과가 좋지 않습니다. 어떤 팁? 어떻게해야합니까?

내가 이벤트 핸들러를 위해 사용했던 코드는 다음과 같습니다 당신은 버튼 스타일을 재정의 할 필요가

private void Border_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     Size secondPage = new Size(); 
     secondPage.Show(); 
     this.Close(); 
    } 

답변

0

, 당신은 ControlTemplate을 사용하여 전송할 수 있습니다. 다음은 단추를 다시 정의하는 재사용 가능한 스타일을 작성하는 예입니다.

IsPressed 이벤트에서 색상 변경을 구현하는 방법에 대한 예제도 추가했습니다.

<Window x:Class="ColorTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
    <ResourceDictionary> 
     <LinearGradientBrush x:Key="ButtonBackground" StartPoint="0,0" EndPoint="1,1"> 
      <GradientStop Color="#FFD9EDFF" Offset="0"/> 
      <GradientStop Color="#FFC0DEFF" Offset="0.445"/> 
      <GradientStop Color="#FFAFD1F8" Offset="0.53"/> 
     </LinearGradientBrush> 
     <Style x:Key="SimpleButtonStyle" TargetType="{x:Type Button}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Button}"> 
         <Grid> 
          <Border Background="{StaticResource ButtonBackground}" VerticalAlignment="Stretch" CornerRadius="2" HorizontalAlignment="Stretch"/> 
          <Border x:Name="BorderPressed" Opacity="0" Background="Blue" VerticalAlignment="Stretch" CornerRadius="2" HorizontalAlignment="Stretch"/> 
          <Label Name="label1" Content="1" HorizontalAlignment="Left"></Label> 
          <Label Name="label2" Content="2" HorizontalAlignment="Right"></Label> 
          <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" x:Name="MainContent" /> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsPressed" Value="True"> 
           <Trigger.EnterActions> 
            <BeginStoryboard> 
             <Storyboard> 
              <DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="1" Duration="0:0:0.2"/> 
             </Storyboard> 
            </BeginStoryboard> 
           </Trigger.EnterActions> 
           <Trigger.ExitActions> 
            <BeginStoryboard> 
             <Storyboard> 
              <DoubleAnimation Storyboard.TargetName="BorderPressed" Storyboard.TargetProperty="Opacity" To="0" Duration="0:0:0.2"/> 
             </Storyboard> 
            </BeginStoryboard> 
           </Trigger.ExitActions> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </ResourceDictionary> 
</Window.Resources> 
<DockPanel> 
    <Button Content="Button" Height="23" Name="button1" Width="75" Style="{StaticResource SimpleButtonStyle}" Click="button1_Click"/> 
</DockPanel> 
관련 문제