Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight 从DataTemplate内部绑定到ICommand_Silverlight_Xaml_Data Binding_Silverlight 4.0_Datatemplate - Fatal编程技术网

Silverlight 从DataTemplate内部绑定到ICommand

Silverlight 从DataTemplate内部绑定到ICommand,silverlight,xaml,data-binding,silverlight-4.0,datatemplate,Silverlight,Xaml,Data Binding,Silverlight 4.0,Datatemplate,我有一个自定义控件,它具有ICommand依赖属性。我想从DataTemplate内部绑定到此属性。为了让事情更清楚,以下是我的代码: 数据表单类: public class DataForm : Form { #region Properties public ICommand HideForm { get { return (ICommand)GetValue(HideFormProperty); } set { SetValue(H

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

数据表单类:

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
}
<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>
<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>
    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);
数据表单的控制模板:

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
}
<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>
<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>
    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绑定到数据模板中的该命令(和其他属性),而无需将该属性(
HideForm
在本例中)添加到
DataForm
类。DataContext不应该被继承吗?理论上,我所说的应该有效吗?

我在中使用,请参见下面的示例代码

<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>

.. 你的代码在这里。。

您可以阅读更多关于的信息,结果表明,除非使用。另一种方法是使用
ControlTemplate
,并绑定到
ContentControl
Template
属性,而不是使用
ContentPresenter

除您之外没有人拥有
表单
文本字段
图像按钮字段
类,因此,除了您之外,没有人可以运行此代码来找出问题所在。特别是,
表单
如何使用
头模板
?卢克·伍德沃德:这很难,因为继承链中有几个类,但我很确定问题出在数据模板上。我想知道我想要实现的是可能的还是Silverlight 4的限制。。。