簡體   English   中英

Caliburn.micro和一個用戶控件單擊處理程序

[英]Caliburn.micro and a usercontrol click handler

我創建了一個非常簡單的用戶控件,一個ImageButton

<UserControl x:Class="SampleApp.Controls.ImageButton"
             Name="ImageButtonControl"
             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"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <Button>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="1*" />
                <RowDefinition Height="6*" />
                <RowDefinition Height="2*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Image Grid.Row="1" Source="{Binding ElementName=ImageButtonControl, Path=Image}" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
            <TextBlock Grid.Row="2" Text="{Binding ElementName=ImageButtonControl, Path=Text}" VerticalAlignment="Stretch" HorizontalAlignment="Center" />
        </Grid>
    </Button>
</UserControl>

后面有代碼:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SampleApp.Controls
{
    /// <summary>
    /// Interaction logic for ImageButton.xaml
    /// </summary>
    public partial class ImageButton : UserControl
    {
        public ImageButton()
        {
            InitializeComponent();
        }

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

        public static readonly DependencyProperty TextProperty =
          DependencyProperty.Register("Text", typeof(string), typeof(ImageButton), new UIPropertyMetadata(""));

        public ImageSource Image
        {
            get { return (ImageSource)GetValue(ImageProperty); }
            set { SetValue(ImageProperty, value); }
        }

        public static readonly DependencyProperty ImageProperty =
           DependencyProperty.Register("Image", typeof(ImageSource), typeof(ImageButton), new UIPropertyMetadata(null)); 
    }
}

現在,我想在我的小示例應用程序中使用它

xmlns:controls="clr-namespace:SampleApp.Controls"

<controls:ImageButton Grid.Row="1"
                              Grid.Column="1"
                              Margin="2"
                              Image="/Images/link.png"
                              Text="DoSomething">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="DoSomething" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </controls:ImageButton>

如果我給它ax:Name,就像

<controls:ImageButton x:Name="DoSomething" 

例如DoSomething的方法DoSomething與名稱直接叫做顯示視圖時,即當我主動包含按鈕,就像我按一下按鈕(如果它是一個正常的按鈕,而不是用戶控件的視圖模型,它的工作是方式),但從不調用按鈕單擊處理程序。

現在,我嘗試添加一個如上所述的ActionMessage,但是它也不起作用...

怎么了

那是因為沒有為您的用戶控件類型配置任何約定。 您可以通過ConventionManager添加約定(請參見http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions ),也可以從Button派生類型。

您也不能使用自定義用戶控件,而只是將圖像添加到視圖中ButtonContent屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM