【问题标题】:OnApplyTemplate doesn't capture the BindingContext of the parent?OnApplyTemplate 没有捕获父级的 BindingContext?
【发布时间】:2019-12-13 21:42:19
【问题描述】:

在一个测试项目中,我试图通过模板绑定获取父控件的BindingContext

这里,在MainPage 中,我有两个模板temp1temp2

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:vm="clr-namespace:MyXam.ViewModels"
             xmlns:views="clr-namespace:MyXam.Views"
             x:Class="MyXam.Views.MainPage"
             x:DataType="vm:MainViewModel">
    <ContentPage.Resources>
        <ResourceDictionary>
            <ControlTemplate x:Key="temp1">
                <views:View1/>
            </ControlTemplate>
            <ControlTemplate x:Key="temp2">
                <views:View2/>
            </ControlTemplate>

        </ResourceDictionary>
    </ContentPage.Resources>
    <StackLayout x:Name="stk">
        <Button Text="Switch view" Command="{Binding SwitchViewCommand}"/>
        <ContentView x:Name="cv" ControlTemplate="{StaticResource temp2}" VerticalOptions="Start" HorizontalOptions="Center">
            <ContentView.Triggers>
                <DataTrigger TargetType="ContentView" Binding="{Binding IsView1}" Value="False">
                    <Setter Property="ControlTemplate" Value="{StaticResource temp2}"/>
                </DataTrigger>
                <DataTrigger TargetType="ContentView" Binding="{Binding IsView1, Mode=TwoWay}" Value="True">
                    <Setter Property="ControlTemplate" Value="{StaticResource temp1}"/>
                </DataTrigger>
            </ContentView.Triggers>
        </ContentView>
    </StackLayout>
</ContentPage>

我想在ctor中获得MainPage中的BindingContext BindingContext

SetBinding(BindingContextProperty, new Binding("Parent.BindingContext", source: RelativeBindingSource.TemplatedParent));

但是当我尝试在OnApplyTemplate 中获取它的 vzlue 时,它​​是空的:

 protected override void OnApplyTemplate()
 {
     base.OnApplyTemplate();
     vm = this.GetValue(BindingContextProperty);
 }

但是绑定在 xaml 中解决:

<Label Text="{Binding Name, Source={Reference this}}"/>

【问题讨论】:

    标签: xamarin.forms data-binding


    【解决方案1】:

    我没有看到你其他代码的细节,但是你可以参考下面的代码。

    xaml.cs 中为这个标签定义一个BindableProperty 并使用名称属性,例如x:Name="TestControlView" 和这样的绑定

     <Label Text="{Binding Source={x:Reference TestControlView}, Path=TestText}" />
    

    您可以查看完整的演示 here。 主要代码是这样的:

    TestControl.xaml.cs(TestControl 是一个 ContentView)

    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class TestControl : ContentView
    {
    
        public static readonly BindableProperty TestTextProperty = BindableProperty.Create(nameof(TestText), typeof(string), typeof(TestControl));
        public string TestText
        {
            get { return (string)GetValue(TestTextProperty); }
            set { SetValue(TestTextProperty, value); }
        }
    
        public TestControl()
        {
            InitializeComponent();
        }        
    }
    

    TestControl.xaml

    MainPage.xaml

     <ListView  x:Name="lstView" HorizontalOptions="Fill" HasUnevenRows="True"  RefreshAllowed="true" IsPullToRefreshEnabled="true">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Frame CornerRadius="10" BackgroundColor="White" Margin="0,5,0,5">
                                  <StackLayout Orientation="Horizontal" HorizontalOptions="Center">
                                                <controls:TestControl TestText="{Binding Title}"  VerticalOptions="Center"/>
                                                <Label Text="{Binding Type}" FontSize="Medium" TextColor="#F0BB7F" FontAttributes="Bold" VerticalOptions="Center"/>
                                  </StackLayout>
                            </Frame>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
    

    MainPage.xaml.cs

     public partial class MainPage : ContentPage
     {
        public ObservableCollection<TestModel> veggies { get; set; }
    
        public MainPage()
        {
            InitializeComponent();
    
            veggies = new ObservableCollection<TestModel>();
    
            veggies.Add(new TestModel { Title = "Tomato", Type = "Fruit" });
            veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });
            veggies.Add(new TestModel { Title = "Tomato" , Type = "Vegetable" });
            veggies.Add(new TestModel { Title = "Romaine", Type = "Fruit" });
            veggies.Add(new TestModel { Title = "Zucchini", Type = "Vegetable" });
    
            lstView.ItemsSource = veggies;
    
            BindingContext = this;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      相关资源
      最近更新 更多