Update ViewCell UI through bound property in the code behind of ViewCell

LeoJHarrisLeoJHarris NZMember ✭✭✭
edited October 2019 in Xamarin.Forms

Hi All,

I have a ViewCell with some bound properties between the .xaml and .xaml.cs of a viewcell. I am attempting to update the UI with a property change that is triggered in the code behind of the viewCell.

My code looks like this:

public static readonly BindableProperty MainTaskStateColorBindableProperty =
           BindableProperty.Create(
               nameof(MainTaskStateColor),
               typeof(Color),
               typeof(TaskWithSubTasksViewCell),
               Color.Transparent);

public static readonly BindableProperty MainTaskStateGlyphBindableProperty =
           BindableProperty.Create(
               nameof(MainTaskStateGlyph),
               typeof(string),
               typeof(TaskWithSubTasksViewCell),
               string.Empty);

public Color MainTaskStateColor { get => (Color)GetValue(MainTaskStateColorBindableProperty); set => SetValue(MainTaskStateColorBindableProperty, value); }

public string MainTaskStateGlyph { get => (string)GetValue(MainTaskStateGlyphBindableProperty); set => SetValue(MainTaskStateGlyphBindableProperty, value); }

and the xaml is like this:

...
<Image
                x:Name="mainTaskCheckBoxImage"
                Grid.Row="0"
                Grid.Column="2"
                HorizontalOptions="End"
                VerticalOptions="Center">
                <Image.Source>
                    <FontImageSource
                        FontFamily="{StaticResource MaterialFontFamily}"
                        **Glyph="{Binding MainTaskStateGlyph}"**
                        Size="{StaticResource MaterialFontSize}"
                        **Color="{Binding MainTaskStateColor}"** />
                </Image.Source>
            </Image>
...

The properties I want changed are the Color and Glyph of the FontImageSource so I have setup bindings directly to these.
Problem is when I run the app nothing updates in the UI when I know infact i should (Ive replaced icons with fonts and it previously worked).

Is this the correct way to update a viewcell from the code behind? The logic is contained in the viewcell that is why I am updating from the xaml.cs not a viewmodel.

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    You want to change the FontImageSource's properties but create the bindable property for the view cell.
    I'm confused about what you want to achieve.
    You can manipulate the models of your Image to change the property with binding. i.e. change the value of MainTaskStateGlyph.
    Moreover, if you want to apply the changing of the BindableProperty you need to handle the propertyChanged like:

    public static readonly BindableProperty MainTaskStateColorBindableProperty =
        BindableProperty.Create(
            nameof(MainTaskStateColor),
            typeof(Color),
            typeof(TaskWithSubTasksViewCell),
            Color.Transparent,
            propertyChanged: (bindableObject, oldValue, newValue) =>
            {
                TaskWithSubTasksViewCell cell = bindableObject as TaskWithSubTasksViewCell;
                // Congure the cell here
            });
    
  • LeoJHarrisLeoJHarris NZMember ✭✭✭

    @LandLu thanks for that, I mainly wanted to set the glyph and color seperately rather then assigning a new FontImageSource. So that will work. I thought I might be able to set MainTaskStateGlyph directly in code behind to update UI rather then in the the propertyChanged function.

  • LandLuLandLu Member, Xamarin Team Xamurai

    If you don't need the feature about the custom bindable property you can change the property directly.
    Glyph and Color are both default Bindable Property of FontImageSource.

Sign In or Register to comment.