如何在用作另一个用户控件中的按钮的用户控件中创建按钮的命令属性 [英] How to create command property of a button in a user control used as buttons in another user control

查看:87
本文介绍了如何在用作另一个用户控件中的按钮的用户控件中创建按钮的命令属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



1.我想创建用户控件按钮的Command和Content等属性,用于另一个用户控件按钮。

2。如何替换用户控件按钮内部使用的图像,并用于其他用户控件的按钮。



在xaml中:创建一个按钮作为用户控件包含包含图像的按钮。



Hi,
1. I want to create the properties like Command and Content of the user control's button which is used for another user control as buttons.
2. How to replace the image which is used inside button of a user control and is used for another user control's buttons.

In xaml: create a button as a user control containing a button which contains an image.

<Button>
  <stackPanel>
    <Button>
      <ButtonStyle>
         <Style TargetType = "{x: Type Button}">
           <setter property = "Template">
             <setter.value>
                <controlTemplate TemplateType = "{x: Type Button}">
                   <ControlPresenter width= "{TemplateBinding Width}"/>
                  
                    </controlTemplate >
                   </setter.value>
                 </setter>
              </Style>
           </ButtonStyle>
             <Image source = ".jpg"  >
         </Button>
   <stackPanel>
</Button

推荐答案

Step : 1 - Create new command as a DepenedecyProperty and implement it 

Steo : 2 -  Bind Command property in the button the the MyButtonCommandProperty in your UserControl.


public Class YourUserControl: UserControl 
 {

      public YourUserControl()
      {
          MyButtonCommad = new RelayCommand
                     (
                         () => { // do some stuff in execute delegate},
                         () => { return true ;}                                                        
                     );
      }

      public bool MyButtonCommad 
      {
           get { return (ICommand)GetValue(MyButtonCommandProperty ); }
           set { SetValue(MyButtonCommandProperty , value); }
      }    

      public static readonly DependencyProperty MyButtonCommandProperty = 
                             DependencyProperty.Register("MyButtonCommad ", typeof(ICommand),new FrameworkPropertyMetadata(null));

 }


// XAML : 

<Window>
      <StackPanel>
          <Button Command={Binding ElementName=control1,Path=MyButtonCommad ,Mode=OneWay/>
          <YourUserControl: x:Name="control1" />
      </StackPanel>
 </Window>


这篇关于如何在用作另一个用户控件中的按钮的用户控件中创建按钮的命令属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆