【问题标题】:UserControl binding to itselfUserControl 绑定到自身
【发布时间】:2016-11-16 12:59:12
【问题描述】:

我创建了一个大的UserControl,出于测试目的,我设置了BindingContext = this。现在我需要一个BindableProperty,所以我不能再使用这个技巧了,那么我该如何设置它来反映自己呢?
问题是,我在 XAML 中使用 UserControl,然后它说,当我设置 BindingContext = this 时,为 SelectedDate 绑定的元素不存在。

UserControl 太复杂了,我可以在 XAML 中定义的唯一的东西就是 main like

<?xml version="1.0" encoding="UTF-8"?>
<Grid xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="ComplexUserControl" />

我没有找到正确的方法

我已经创建了一个简单的 UserControl,所以你可以看到我想要做什么

class Test : Grid
{
   public static readonly BindableProperty SelectedDateProperty = BindableProperty.Create("SelectedDate", typeof(DateTime), typeof(Test), defaultValue: DateTime.Now, defaultBindingMode: BindingMode.TwoWay);

   public DateTime Date1
   {
      get { return DateTime.Today.AddDays(-1); }
   }

   public DateTime Date2
   {
      get { return DateTime.Today; }
   }

   public DateTime Date3
   {
      get { return DateTime.Today.AddDays(1); }
   }

   public Test()
   {
      InitializeComponent();
   }

   private void InitializeComponent()
   {
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

      Button btn1 = new Button();
      btn1.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date1));
      btn1.SetBinding(Button.TextProperty, new Binding("Date1", stringFormat: "dd"));
      Children.Add(btn1, 0, 0);

      Button btn2 = new Button();
      btn2.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date2));
      btn2.SetBinding(Button.TextProperty, new Binding("Date2", stringFormat: "dd"));
      Children.Add(btn2, 1, 0);

      Button btn3 = new Button();
      btn3.SetBinding(Button.FontSizeProperty, new Binding("SelectedDate", converter: new IsSelectedFontSizeConverter(), converterParameter: Date3));
      btn3.SetBinding(Button.TextProperty, new Binding("Date3", stringFormat: "dd"));
      Children.Add(btn3, 2, 0);
   }
}

应该可以从外部将 SelectedDate 设置为另一个日期。

【问题讨论】:

    标签: c# xamarin binding xamarin.forms


    【解决方案1】:

    是的,你可以。使用 x:Reference。

    <Button Text="{Binding Date3,Source={x:Reference testControl}"
            FontSize="{Binding SelectedDate, Converter={StaticResource myConverter},
                 ConverterParameter={Binding Date3,Source={x:Reference testControl}}}" />
    

    下面的链接可以帮助您理解这个概念。 https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_binding_basics/

    【讨论】:

    • 如何在c#代码中设置testcontrol?因为我无法在 XAML 中创建 UserControl(在 XAML 中的 UserControl 中进行了太多计算)?
    • 您必须设置控件的 x:name="testControl"。创建 test.xaml 页面。
    • 如我所见,只能在 xaml 中完成
    • 是的,我不知道如何通过代码实现这一点。我有同样的问题,我用这种方法解决了。
    • 然后我必须将 UserControl 创建为 xaml,其中我唯一可以定义的是 Grid :o
    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2013-06-03
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多