Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.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
C# 具有多个内容演示者的ControlTemplate。如何将ContentPresenter的内容绑定到Xamarin表单中DataTemplate的值_C#_Xaml_Xamarin_Mvvm_Xamarin.forms - Fatal编程技术网

C# 具有多个内容演示者的ControlTemplate。如何将ContentPresenter的内容绑定到Xamarin表单中DataTemplate的值

C# 具有多个内容演示者的ControlTemplate。如何将ContentPresenter的内容绑定到Xamarin表单中DataTemplate的值,c#,xaml,xamarin,mvvm,xamarin.forms,C#,Xaml,Xamarin,Mvvm,Xamarin.forms,我有一个自定义控件Xamarin Forms,看起来像这样: public class CardView : Frame { public static readonly BindableProperty HeaderDataTemplateProperty = BindableProperty.Create("HeaderDataTemplate", typeof(DataTemplate), typeof(HeaderCardView), null, BindingMode.TwoW

我有一个自定义控件Xamarin Forms,看起来像这样:

public class CardView : Frame
{
    public static readonly BindableProperty HeaderDataTemplateProperty = BindableProperty.Create("HeaderDataTemplate", typeof(DataTemplate), typeof(HeaderCardView), null, BindingMode.TwoWay);
    public static readonly BindableProperty BodyDataTemplateProperty = BindableProperty.Create("BodyDataTemplate", typeof(DataTemplate), typeof(HeaderCardView));

    public DataTemplate HeaderDataTemplate
    {
        get => (DataTemplate)GetValue(HeaderDataTemplateProperty);
        set => SetValue(HeaderDataTemplateProperty, value);
    }

    public DataTemplate BodyDataTemplate
    {
        get => (DataTemplate)GetValue(BodyDataTemplateProperty);
        set => SetValue(BodyDataTemplateProperty, value);
    }

    public CardView()
    {
        if (Device.RuntimePlatform.Equals("iOS"))
        {
            HasShadow = false;
            OutlineColor = Color.Transparent;
            BackgroundColor = Color.Transparent;
        }
    }
}
其思想是创建一个“卡片”控件,该控件可以为标题和正文定义两个数据模板

因此,我定义了一个ControlTemplate,它使用两个内容演示者:

<ControlTemplate x:Key="HeaderCardControlTemplate">
    <StackLayout>
        <ContentView Padding="5" BackgroundColor="#a6192e">
            <ContentPresenter Content="{TemplateBinding Parent.HeaderDataTemplate, Mode=TwoWay}"></ContentPresenter>
        </ContentView>
        <ContentView>
            <ContentPresenter Content="{TemplateBinding Parent.BodyDataTemplate}"></ContentPresenter>
        </ContentView>
     </StackLayout>
</ControlTemplate>

然后像这样使用它:

<controls:HeaderCardView ControlTemplate="{StaticResource HeaderCardControlTemplate}">                                        
    <controls:HeaderCardView.HeaderDataTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label FontAttributes="Bold" Text="{Binding Value}" Grid.Row="0" Grid.Column="0"></Label>
                <Label FontAttributes="Bold, Italic" Text="{Binding Type}" HorizontalOptions="End" Grid.Row="0" Grid.Column="1"></Label>
            </Grid>
        </DataTemplate>                                    
    </controls:HeaderCardView.HeaderDataTemplate>
</controls:HeaderCardView>

但是,ContentPresenter没有显示假定为网格的HeaderDataTemplate


你知道我遗漏了什么或做错了什么吗?

嘿,你能找到解决方案吗?嗨@RodrigoJuarez,我记得我必须使用BindableProperty中的BindingPropertyChangeDelegate pram进行一些手动设置。当数据模板更改时,我会创建一个新视图,并在内部手动设置它。