如何使自定义控件的元素在视图上可访问以将其订阅到命令?

How to make an Element of a Custom Control Accessible on the View to Subscribe it to a Command?

我有一个自定义 window,它有两个按钮。一个按钮被命名为 OKButton,另一个被命名为 Cancel Button。

<Style TargetType="{x:Type WindowCustom}">
    "Properties Here"
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type WindowCustom}">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                                    <Button x:Name="OKButton" Content="OK"/>
                                    <Button x:Name="CancelButton" Content="Cancel"/> 
"Closing Tags"

我已经为在 OnApplyTemplate 方法中实例化的 "OKButton" 使用 CLR 属性创建了一个模板部分。

 private const string OKButtonPart = "PART_OKButton";

    private Button oKButton;

    public Button OKButton
    {
        get { return oKButton; }
        set
        {
            if (oKButton != null)
            {
                oKButton.Click += OKButtonClick;
                oKButton.Loaded += OKButtonLoaded;
            }

            oKButton = value;
        }
    }
    public override void OnApplyTemplate()
    {
        OKButton = GetTemplateChild(OKButtonPart) as Button;
    }

假设创建自定义 window 所需的所有其他代码都已存在。我写了几个路由命令来让我的 OKButton 做我想做的事。这并不理想,因为我之前的按钮实现使用了 ActionMessage(Caliburns 的命令方式)

<Button>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="SaveHistoryEntry" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

如何通过 XAML 访问我的控件以将其添加到操作消息中? 我能做的是在自定义 window.

上写下我的按钮控件的名称
<lc:WindowCustom OKButton="">

我不知道从这里可以做什么。

您可以将依赖项 属性 添加到您的 WindowCustom class:

public static readonly DependencyProperty OkCommandProperty =
     DependencyProperty.Register("OkCommand", typeof(ICommand),
     typeof(CustomWindow), new FrameworkPropertyMetadata(null));

public ICommand OkCommand
{
    get { return (ICommand)GetValue(OkCommandProperty); }
    set { SetValue(OkCommandProperty, value); }
}

...并将 ControlTemplateButtonCommand 属性 绑定到这个:

<Button x:Name="OKButton" Content="OK" Command="{Binding OkCommand, RelativeSource={RelativeSource TemplatedParent}}"/>

然后您可以像往常一样将 window 的依赖项 属性 设置或绑定到任何 ICommand 源 属性:

<lc:WindowCustom OkCommand="{Binding YourViewModelCommandProperty}">

单击 Button 时将调用该命令。您当然可以对取消 Button 做同样的事情。只需添加另一个依赖项 属性.

此答案针对的是希望在其自定义控件上使用 ActionMessage 功能的 Caliburn 用户。 我自定义 window 上的按钮看起来像这样

<lc:ButtonCustom x:Name="PART_OKButton">
             <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <cal:ActionMessage MethodName="{Binding OkCommand, RelativeSource={RelativeSource TemplatedParent}}" />
                 </i:EventTrigger>
              </i:Interaction.Triggers>
 </lc:ButtonCustom>

CustomWindow 上的C# 代码与mm8 的答案几乎相同。

public string OkCommand
    {
        get { return (string)GetValue(OkCommandProperty); }
        set { SetValue(OkCommandProperty, value); }
    }



public static readonly DependencyProperty OkCommandProperty = DependencyProperty.Register("OkCommand", typeof(string), typeof(WindowCustom), 
            new FrameworkPropertyMetadata(null));

我将 ICommand 更改为字符串数据类型,因为 ActionMessage 接受字符串。

最后在 window 上,我将我想要的操作分配给操作消息。

<lc:WindowCustom <!--xmlns tags and other dependency proerties-->
    OkCommand="SaveHistoryEntry">

有效!