Привязка к вычисляемому свойству BindableProperty из кодаC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Гость
 Привязка к вычисляемому свойству BindableProperty из кода

Сообщение Гость »


In short, I have a custom control in Xamarin for which I defined a BindableProperty, which I would like to bind to the control's xaml via a calculated property in the code behind, but it always displays as null because the calculated property is evaluated when the BindableProperty didn't have a value yet, and afterward I am missing property changed notification logic. Which is the right way to do it?
Long explanation for context:
I am writing a custom control in Xamarin (which I named

Код: Выделить всё

TabbedLayout
). I intend to bind an array of Views to a BindableProperty of the control, like so: To that effect I have defined a BindableProperty in the code behind the Control (TabbedLayout.xaml.cs):

Код: Выделить всё

public static readonly BindableProperty TabContentsProperty = BindableProperty.Create(
nameof(TabContents),
typeof(View[]),
typeof(TabbedLayout),
null,
BindingMode.OneWay);

public View[] TabContents
{
get => (View[])GetValue(TabContentsProperty);
set => SetValue(TabContentsProperty, value);
}
However, from the control's xaml I wish to display only one of those Views. I have first defined a int property to control which is the selected index (with other parts of the control, let's assume that

Код: Выделить всё

int SelectedIndex = 0
and I wish to display the first element).
I have then created a calculated property like so:

Код: Выделить всё

public View DisplayedElement = TabContents?.ElementAt(SelectedIndex);
And I have bonded to it from the control's xaml like so: At this point, the ContentView displays empty because TabContents is still null.
From here, how would I update the view as soon as the BindableProperty value is set, the first time and every time it might be modified, to trigger a refresh of the bound DisplayedElement property?


Источник: https://stackoverflow.com/questions/781 ... ode-behind
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Привязка к вычисляемому свойству BindableProperty из кода
    Гость » » в форуме C#
    0 Ответы
    32 Просмотры
    Последнее сообщение Гость
  • Привязка к пользовательскому BindableProperty не работает
    Anonymous » » в форуме C#
    0 Ответы
    17 Просмотры
    Последнее сообщение Anonymous
  • Сортировка строк набора результатов запроса к базе данных по сложному вычисляемому значению
    Anonymous » » в форуме Php
    0 Ответы
    15 Просмотры
    Последнее сообщение Anonymous
  • Использование z3 для поиска графиков, не соответствующих свойству A и свойству B.
    Anonymous » » в форуме Python
    0 Ответы
    27 Просмотры
    Последнее сообщение Anonymous
  • Использование z3 для поиска графиков, не соответствующих свойству A и свойству B.
    Anonymous » » в форуме Python
    0 Ответы
    20 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»