2012-09-18 3 views
2

사용자 지정 UserControl을 만들고 있는데 UC가 Button처럼 Command을 갖기를 바랍니다. 나는 WPF를 처음 사용한다.WPF : UserControl의 명령 및 CommandParameter

WelcomeView.xaml

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="150"/> 
    </Grid.RowDefinitions> 

    <Controls:SignedButton Grid.Row="1" VerticalAlignment="Top" Width="245" Height="45" Foreground="#FFFFFF" 
          LeftSign="+" Text="Add an account" TextSize="20" 
          ButtonBackground="#3A5795" HoverBackground="#C41AD7" HoverOpacity="1" 
          Command="{x:Static Infrastructure:ApplicationCommands.NavigateCommand}" 
          CommandParameter="{x:Type Views:AddAccountView}"/> 
</Grid> 

SignedButton.xaml.cs

public partial class SignedButton : UserControl 
{ 

    public static DependencyProperty ButtonBackgroundProperty = 
     DependencyProperty.Register("ButtonBackground", typeof (string), typeof (SignedButton)); 

    public static DependencyProperty CommandParameterProperty = 
     DependencyProperty.Register("CommandParameter", typeof (object), typeof (SignedButton)); 

    public static DependencyProperty CommandProperty = 
     DependencyProperty.Register("Command", typeof (ICommand), typeof (SignedButton)); 

    public static DependencyProperty HoverBackgroundProperty = 
     DependencyProperty.Register("HoverBackground", typeof (string), typeof (SignedButton)); 

    public static DependencyProperty HoverOpacityProperty = 
     DependencyProperty.Register("HoverOpacity", typeof (double), typeof (SignedButton)); 

    public static DependencyProperty LeftSignProperty = 
     DependencyProperty.Register("LeftSign", typeof (string), typeof (SignedButton)); 

    public static DependencyProperty RightSignProperty = 
     DependencyProperty.Register("RightSign", typeof (string), typeof (SignedButton)); 

    public static DependencyProperty TextProperty = 
     DependencyProperty.Register("Text", typeof (string), typeof (SignedButton)); 

    public static DependencyProperty TextSizeProperty = 
     DependencyProperty.Register("TextSize", typeof (double), typeof (SignedButton)); 

    public SignedButton() 
    { 
     InitializeComponent(); 
    } 

    public string ButtonBackground 
    { 
     get { return (string) GetValue(ButtonBackgroundProperty); } 
     set { SetValue(ButtonBackgroundProperty, value); } 
    } 

    public ICommand Command 
    { 
     get { return (ICommand) GetValue(CommandProperty); } 
     set { SetValue(CommandProperty, value); } 
    } 

    public object CommandParameter 
    { 
     get { return GetValue(CommandParameterProperty); } 
     set { SetValue(CommandParameterProperty, value); } 
    } 

    public string HoverBackground 
    { 
     get { return (string) GetValue(HoverBackgroundProperty); } 
     set { SetValue(HoverBackgroundProperty, value); } 
    } 

    public double HoverOpacity 
    { 
     get { return (double) GetValue(HoverOpacityProperty); } 
     set { SetValue(HoverOpacityProperty, value); } 
    } 

    public string LeftSign 
    { 
     get { return (string) GetValue(LeftSignProperty); } 
     set { SetValue(LeftSignProperty, value); } 
    } 

    public string RightSign 
    { 
     get { return (string) GetValue(RightSignProperty); } 
     set { SetValue(RightSignProperty, value); } 
    } 

    public string Text 
    { 
     get { return (string) GetValue(TextProperty); } 
     set { SetValue(TextProperty, value); } 
    } 

    public double TextSize 
    { 
     get { return (double) GetValue(TextSizeProperty); } 
     set { SetValue(TextSizeProperty, value); } 
    } 
} 

SignedButton.xaml을 :

내가 운이없이 지금까지 시도한 것입니다

<UserControl x:Class="Client.Infrastructure.Controls.SignedButton" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      mc:Ignorable="d" 
      d:DesignHeight="45" d:DesignWidth="245" 
      DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <UserControl.Resources> 
     <FontFamily x:Key="OpenSans">. . .</FontFamily> 
     <Storyboard x:Key="OnMouseEnter"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> 
       . . . 
      </DoubleAnimationUsingKeyFrames> 
      <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid"> 
       . . . 
      </ColorAnimationUsingKeyFrames> 
     </Storyboard> 
     <Storyboard x:Key="OnMouseLeave"> 
      <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid"> 
       . . . 
      </DoubleAnimationUsingKeyFrames> 
      <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)" Storyboard.TargetName="grid"> 
       . . . 
      </ColorAnimationUsingKeyFrames> 
     </Storyboard> 
    </UserControl.Resources> 
    <UserControl.Triggers> 
     <EventTrigger RoutedEvent="Mouse.MouseEnter"> 
      . . . 
     </EventTrigger> 
     <EventTrigger RoutedEvent="Mouse.MouseLeave"> 
      . . . 
     </EventTrigger> 
    </UserControl.Triggers> 
    <Grid x:Name="grid" Cursor="Hand" Background="{Binding ButtonBackground}"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="50"/> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="50"/> 
     </Grid.ColumnDefinitions> 
     <TextBlock Text="{Binding LeftSign}" 
        FontFamily="{DynamicResource OpenSans}" FontSize="{Binding TextSize}" 
        HorizontalAlignment="Center" VerticalAlignment="Center"/> 
     <TextBlock Text="{Binding Text}" 
        FontFamily="{DynamicResource OpenSans}" FontSize="{Binding TextSize}" 
        HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/> 
     <TextBlock Text="{Binding RightSign}" 
        FontFamily="{DynamicResource OpenSans}" FontSize="{Binding TextSize}" 
        HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="2"/> 
    </Grid> 
</UserControl> 

답변

5

왼쪽의 한 단계는 ICommand 인터페이스를 컨트롤의 UI에 연결하는 것입니다. 마우스 클릭 이벤트 리스너를 작성하여 시작하십시오.

public SignedButton() 
{ 
    InitializeComponent(); 
    this.MouseUp += new MouseButtonEventHandler(MyClassContent_MouseUp); 
} 

void MyClassContent_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    Command.Execute(CommandParameter); 
} 

은 또한 UI에 클릭 할 수있는 표시를 활성화/비활성화하려면 Command 인스턴스에서 CanExecuteChanged 이벤트를 수신 할 수 있습니다. 질문의 범위에서


각주, 다양한 기능을 다시 발명보다는 WPF Button 컨트롤을 확장하는 것이 좋습니다 수 있습니다. customize its appearance and behavior in various ways은 여전히 ​​Visual States (누르기, 활성, 비활성화 등) 및 ICommand 통합과 같은 핵심 기능을 사용하는 것이 가능합니다.

관련 문제