user3911053
user3911053

Reputation:

XAML Binding Not Updating

I have a GridView component which is holding my projects. When a project is selected I want controls to pop up over it, allowing you to edit or delete the project. Here is the DataTemplate I'm using for the items:

<Grid Height="200" Width="200">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Row="0" Fill="White"/>
    <TextBlock Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="No preview image" FontSize="18" Foreground="{ThemeResource TextBoxDisabledBorderThemeBrush}"/>
    <Image Style="{StaticResource ProjectCoverImageStyle}" Grid.Row="0"/>
    <TextBlock Style="{StaticResource ProjectTitleStyle}" Text="{Binding Title}" Grid.Row="1"/>
    <!--The controls for editing and deleting. Show only when project is selected.-->
    <Grid x:Name="ControlsGrid" Visibility="{Binding IsCurrent, Converter={StaticResource VisibilityConverter}}">
        <Rectangle Grid.Row="0" Fill="Gray" Opacity="0.5"/>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
            <Border Style="{StaticResource RoundButtonStyle}">
                <Image Source="/Assets/Icons/Edit.png" Style="{StaticResource RoundButtonIconStyle}"/>
            </Border>
            <Border Style="{StaticResource RoundButtonStyle}" Tapped="Delete_Project">
                <Image Source="/Assets/Icons/Delete.png" Style="{StaticResource RoundButtonIconStyle}"/>
            </Border>
        </StackPanel>
    </Grid>
</Grid>

I'm defining the converter like this:

<local:VisibilityConverter x:Key="VisibilityConverter"/>

VisibilityConverter is a class:

public class VisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        bool visibility = (bool)value;
        return visibility ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        Visibility visibility = (Visibility)value;
        return (visibility == Visibility.Visible);
    }
}

Selecting an item in the GridView runs this:

private void Projects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count < 1)
        return;

    foreach (Project project in ProjectsGridView.Items)
    {
        if (ProjectsGridView.SelectedItem != project)
            project.IsCurrent = false;
        else
        {
            project.IsCurrent = true;
            ViewModel.CurrentProject = ViewModel.Projects[ProjectsGridView.SelectedIndex];
        }
    }
}

If I set IsCurrent to true manually, it works just fine. However, it doesn't update when selecting / deselecting a project. My code sets IsCurrent correctly, but the binding doesn't seem to update. Is there something else that needs to be done?

Upvotes: 2

Views: 768

Answers (1)

ΩmegaMan
ΩmegaMan

Reputation: 31616

If I set IsCurrent to true manually, it works just fine

For the xaml to be notified of such a change, the class which holds IsCurrent needs to adhere to INotifyPropertyChange and the setter for IsCurrent needs to kick off a property change notification. Then it will work manually as well as programmatically.

Upvotes: 2

Related Questions