Xamarin ControlTemplate设置BindingContext

时间:2018-11-22 00:22:37

标签: xaml xamarin xamarin.forms

我有一个contentcontrol,因此不必编写重复的代码。 现在,我想在一页上两次显示contentcontrol,但是内容不同。如何设置ContentControl的绑定上下文,使其在第一个ContentView中显示firstItem.Title,在第二个ContentView中显示secondItem.Title?

缩短代码,只是为了查看我的意思:

    <ControlTemplate x:Name="ContainerControl">
        <StackLayout>
            <Label Text="{Binding Title}" />
        </StackLayout>
    </ControlTemplate>

    <!-- Display the first item -->
    <ContentView Item="{Binding firstItem}" ControlTemplate="{StaticResource ContainerControl" />

    <!-- Display the second item -->
    <ContentView Item="{Binding secondItem}" ControlTemplate="{StaticResource ContainerControl" />

Item Class / ViewModel属性:

public class Item
{
    public string Title { get; set; }
}

public Item firstItem
{
    get;
    set;
}

public Item secondItem
{
    get;
    set;
}

0 个答案:

没有答案