【问题标题】:Caliburn.micro and a usercontrol click handlerCaliburn.micro 和用户控件点击处理程序
【发布时间】:2013-04-05 19:19:28
【问题描述】:

我创建了一个非常简单的用户控件,一个 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>

如果我给它一个 x:Name,比如

<controls:ImageButton x:Name="DoSomething" 

例如DoSomething 在显示视图时直接调用具有该名称的方法DoSomething,即当我激活包含该按钮的视图模型时,就像我单击按钮一样(如果它是普通按钮而不是用户控件,它将起作用那种方式),但按钮单击处理程序永远不会在单击时调用。

现在我尝试添加如上所示的 ActionMessage,但它也不起作用...

这里有什么问题?

【问题讨论】:

    标签: c# wpf user-controls caliburn.micro


    【解决方案1】:

    这是因为没有为您的用户控件类型配置约定。您可以通过ConventionManager 添加约定(请参阅http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Conventions),或者您可以从Button 派生您的类型。

    您也不能使用自定义用户控件,而只需将图像添加到视图中ButtonContent 属性。

    【讨论】:

    • 是的好主意,我倾向于使用附加属性来设置按钮的图像;这样您就不需要派生控件来制作模板按钮,您只需将默认样式应用于所有按钮。该样式可以包含一个绑定到该附加属性的模板,因此按钮本身不需要知道它 - 它非常有效(但如果我记得,在 VS2010 中的设计器中存在一些与附加属性绑定的问题 - 工作正常但在运行时)
    • 是的,该方法效果很好,您还可以使附加属性足够通用,以便它支持可以模板化的其他属性(例如文本、边框、背景等),以便可以重用一个附加属性跨所有按钮模板样式
    • 非常感谢,使它成为按钮而不是用户控件效果很好。如何制作通用附加属性?我不想将图像添加到每个按钮,因为我想要更简洁的 XAML。
    • @devdigital:你能告诉我如何为我的用户控件配置约定吗?我发现了这个:AddElementConvention&lt;ButtonBase&gt;(ButtonBase.ContentProperty, "DataContext", "Click"); 并认为这会导致我描述的行为。我必须做的那个约定是什么?我不希望我的按钮使用 ViewModel,它只会让我的 XAML 更简洁。
    • 上面的链接描述了约定管理器的工作原理,但在这个用例中,自定义用户控件不是最佳解决方案,因为它不提供附加属性的灵活性。
    猜你喜欢
    • 1970-01-01
    • 2015-03-28
    • 1970-01-01
    • 2010-12-08
    • 2016-04-20
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多