2014-09-07 3 views
0

의 사용자 정의 버튼 컨트롤을 만들기 : 새 프로젝트를 만들 새 사용자 컨트롤을 추가, dll을과에 대한 참조를 추가 구축 내 사람이해야 할 수 있다면 :) 내 응용 프로그램의 XAML 페이지에서</p>이 <hr> <p>이 좋아, 감사 스콧 니므롯, 나는 방법을 찾을 ... WP8

를 응용 프로그램 프로젝트

여기, 내 코드의

<local:ButtonImage Width="40" Height="40" 
         Click="ButtonImage_Click" 
         SourceNormal="/download_home.png"        
         SourcePressed="/download_nowplaying.png"/> 
(클릭 이벤트가 작동하지만이 이미지 "sourceNormal는"가시 "를 sourcePressed"하지 않음) 뒤에

UserControl을 XAML

<UserControl x:Class="MyCustomControl.ButtonImage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
d:DesignHeight="480" d:DesignWidth="480" 

DataContext="{Binding RelativeSource={RelativeSource Self}}"   
xmlns:ntd="clr-namespace:MyCustomControl"> 

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Button x:Name="Button_Image" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 
     <Button.Template> 
      <ControlTemplate TargetType="Button"> 
       <Grid Background="Transparent"> 
        <VisualStateManager.VisualStateGroups> 
         <VisualStateGroup x:Name="CommonStates"> 
          <VisualState x:Name="Normal"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Collapsed</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
          <VisualState x:Name="MouseOver"/> 
          <VisualState x:Name="Pressed"> 
           <Storyboard> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImageNormal"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Collapsed</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ImagePressed"> 
             <DiscreteObjectKeyFrame KeyTime="0"> 
              <DiscreteObjectKeyFrame.Value> 
               <Visibility>Visible</Visibility> 
              </DiscreteObjectKeyFrame.Value> 
             </DiscreteObjectKeyFrame> 
            </ObjectAnimationUsingKeyFrames> 
           </Storyboard> 
          </VisualState> 
         </VisualStateGroup> 
        </VisualStateManager.VisualStateGroups> 
        <Image x:Name="ImageNormal" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourceNormal}" Stretch="Uniform"/> 
        <Image x:Name="ImagePressed" VerticalAlignment="Center" HorizontalAlignment="Center" Source="{Binding SourcePressed}" Stretch="Uniform"/>       
       </Grid> 
      </ControlTemplate> 
     </Button.Template> 
    </Button> 

</Grid> 

UserControl을 코드 ​​:

namespace MyCustomControl 
{ 
public partial class ButtonImage : UserControl 
{ 
    public ButtonImage() 
    { 
     InitializeComponent();    
     Button_Image.Click += ButtonImage_Click; 
    } 

    public ImageSource SourceNormal 
    { 
     get { return (ImageSource)GetValue(SourceNormalProperty);} 
     set { SetValue(SourceNormalProperty, value); } 
    }   
    public ImageSource SourcePressed 
    { 
     get { return (ImageSource)GetValue(SourcePressedProperty); } 
     set { SetValue(SourcePressedProperty, value); } 
    } 

    public event EventHandler Click; 
    void ButtonImage_Click(object sender, RoutedEventArgs e) 
    { 
     var eventHandler = this.Click; 
     if (eventHandler != null) 
     { 
      eventHandler(this, e); 
     } 
    } 



    /////////////////////// SourceNormal ///////////////////////////////////////////////////// 

    //---------------------------------------------------------------------------------------- 
    public static readonly DependencyProperty SourceNormalProperty = DependencyProperty.RegisterAttached(
     "SourceNormal", 
     typeof(ImageSource), 
     typeof(ButtonImage), 
     new PropertyMetadata(null) 
    ); 

    public static ImageSource GetSourceNormal(UIElement element) 
    { 
     if (element == null) 
     { 
      new ArgumentNullException("element"); 
     } 
     return (ImageSource)element.GetValue(SourceNormalProperty); 
    } 

    public static void SetSourceNormal(UIElement element, ImageSource value) 
    { 
     if (element == null) 
     { 
      new ArgumentNullException("element"); 
     } 

     element.SetValue(SourceNormalProperty, value); 
    } 
    //---------------------------------------------------------------------------------------- 

    /////////////////////// SourcePressed //////////////////////////////////////////////////// 

    //---------------------------------------------------------------------------------------- 
    public static readonly DependencyProperty SourcePressedProperty = DependencyProperty.RegisterAttached(
     "SourcePressed", 
     typeof(ImageSource), 
     typeof(ButtonImage), 
     new PropertyMetadata(null) 
    ); 

    public static ImageSource GetSourcePressed(UIElement element) 
    { 
     if (element == null) 
     { 
      new ArgumentNullException("element"); 
     } 
     return (ImageSource)element.GetValue(SourcePressedProperty); 
    } 

    public static void SetSourcePressed(UIElement element, ImageSource value) 
    { 
     if (element == null) 
     { 
      new ArgumentNullException("element"); 
     } 

     element.SetValue(SourcePressedProperty, value); 
    } 
} 

은}

+0

을 I는이에 대한 정말 좋은 옵션이라고 생각하지 말아. 이미지가 작아서 base64 문자열로 변환 될 수 있다고 가정합니다. 당신은 base64 문자열을 bitmap 이미지로 변환하여 DependencyProperty의 기본값으로 만들 수 있습니다. –

+0

아, 그게 아닙니다. :) 내 다른 앱에서 사용할 수있는 새로운 "버튼"을 만들 수 있는지 알고 싶다는 뜻입니다. , WP 툴킷 또는 coding4fun 툴킷과 같은 패키지의 컨트롤 종류 – user3448806

답변

1

대답은 Yes입니다.

Telerik은 RadControls에서 이것을 수행합니다.

별도의 프로젝트를 만들고 WPF가 UI에 사용하는 주요 참조를 추가하기 만하면됩니다. 프리즘 모듈을 구현할 때의 패턴이기도합니다. 다른 프로젝트에서

, 당신의 스타일에 정의 된 DLL에 대한 참조를 추가합니다.

+1

감사합니다. – user3448806

관련 문제