从DataTemplate内部绑定到ICommand

时间:2011-10-17 07:51:20

标签: silverlight xaml data-binding silverlight-4.0 datatemplate

我有一个具有ICommand依赖属性的自定义控件。我想从DataTemplate内部绑定到此属性。为了让事情更清楚,我的代码是:

DataForm类:

public class DataForm : Form
{
    #region Properties

    public ICommand HideForm
    {
        get { return (ICommand)GetValue(HideFormProperty); }
        set { SetValue(HideFormProperty, value); }
    }

    public static readonly DependencyProperty HideFormProperty =
        DependencyProperty.Register("HideForm", typeof(ICommand), typeof(DataForm), new PropertyMetadata(null));


    #endregion

    #region Ctors
    public DataForm(Guid formId, String title)
        : base(formId, title)
    {
        this.Template = Application.Current.Resources["DataFormDefaultTemplate"] as ControlTemplate;
    }
    public DataForm()
        : this(Guid.NewGuid(), "bla")
    {
    }
    #endregion

    #region Methods
    public override void OnApplyTemplate()
    {
        if (HeaderTemplate == null)
        {
            HeaderTemplate = Application.Current.Resources["DefaultFormHeaderTemplate"] as DataTemplate;
        }
        base.OnApplyTemplate();
    }
    #endregion
}

DataForm的ControlTemplate:

<ControlTemplate x:Name="DataFormDefaultTemplate" TargetType="my:DataForm">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ContentPresenter Grid.Row="0" ContentTemplate="{TemplateBinding HeaderTemplate}"></ContentPresenter>
        <ContentPresenter Grid.Row="1" ContentTemplate="{TemplateBinding BodyTemplate}"></ContentPresenter>
        <ContentPresenter Grid.Row="2" ContentTemplate="{TemplateBinding FooterTemplate}"></ContentPresenter>
    </Grid>
</ControlTemplate>

我想要绑定到HideForm命令的DataTemplate:

<DataTemplate x:Name="DefaultFormHeaderTemplate">
    <Grid HorizontalAlignment="Stretch">
        <my:TextField FieldViewStyle="{StaticResource formLabelStyle}" Value="Form Title" Mode="View"></my:TextField>
        <my:ImageButtonField ImagePath="../Images/Popup_Close.png" 
                         CommandToExecute="{Binding HideForm}" 
                             HorizontalAlignment="Right" Width="8" Height="8"></my:ImageButtonField>
    </Grid>
</DataTemplate>

注意:CommandToExecute在ImageButtonField上运行正常。我已经测试了它。

实例化DataForm的代码:

    DataForm form = new DataForm();
    form.BodyTemplate = Application.Current.Resources["DataFormBodyTemplate"] as DataTemplate;
    form.FooterTemplate = Application.Current.Resources["DataFormFooterTemplate"] as DataTemplate;

    form.HideForm = new DelegateCommand(CloseIt);

我如何让它工作?基本上,完美的方案是能够从 ViewModel 绑定到数据模板中的此命令(和其他属性),而不必添加该属性(在这种情况下为HideFormDataForm课程。{{1}}。不应该继承 DataContext - 理论上 - 我说的应该有用吗?

2 个答案:

答案 0 :(得分:0)

我在EventToCommand中使用MVVM Light,请参阅下面的示例代码。

<UserControl.Resources>
    <ContentControl x:Key="ViewModel" Content="{Binding}" />
</UserControl.Resources>

.. Your code here ..

<my:ImageButtonField ImagePath="../Images/Popup_Close.png" 
                     CommandToExecute="{Binding HideForm}" 
                     HorizontalAlignment="Right" Width="8" Height="8">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonUp">
            <Command:EventToCommand Command="{Binding Content.YourCommand, Source={StaticResource ViewModel}}" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</my:ImageButtonField>

您可以阅读有关EventToCommand&lt; - here。

的更多信息

答案 1 :(得分:0)

事实证明,除非您使用Binding Proxy,否则无法使用DataTemplate完成此操作。另一种方法是改为使用ControlTemplate并绑定Template的{​​{1}}属性,而不是使用ContentControl