microsoft / XamlBehaviors

This is the official home for UWP XAML Behaviors on GitHub.
MIT License
697 stars 112 forks source link

UWP DataTriggerBehavior not updating after initial update. #162

Closed jgozner closed 5 years ago

jgozner commented 5 years ago

I have 2 DataTemplates. One for editing and one for previewing. The binding in these templates works great. They all start in preview template and when I click edit, it sets the selected questionId and then the appropriate item switches to the edit template. But when I click on another edit item the original one should go back to preview but it doesn't.

View

<DataTemplate x:Key="EditMode">
    <local:QuestionControl {Binding}/>
</DataTemplate>

<DataTemplate x:Key="PreviewMode">
    <StackPanel>
        <TextBlock x:Name="QuestionId" Text="{Binding QuestionId}"/>
        <TextBlock Text="{Binding Text}"/>
        <Button Content="Edit"
                Command="{Binding EditPageViewModel.SelectQuestionCommand, Source={StaticResource Locator}}" 
                CommandParameter="{Binding}"/>
    </StackPanel>
</DataTemplate>
<ItemsControl x:Name="ItemControl"
                ItemsSource="{Binding SurveyTreeModel[0].Questions}"
                AllowDrop="True">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl Content="{Binding}" 
                            ContentTemplate="{StaticResource PreviewMode}">
                <Interactivity:Interaction.Behaviors>
                    <Interactions:DataTriggerBehavior Binding="{Binding EditPageViewModel.SelectedQuestionId, 
                                                                Source={StaticResource Locator},
                                                                Mode=OneWay}"  ComparisonCondition="Equal" Value="{Binding QuestionId}">
                        <Interactions:ChangePropertyAction PropertyName="ContentTemplate" Value="{StaticResource EditMode}"/>
                    </Interactions:DataTriggerBehavior>
                </Interactivity:Interaction.Behaviors>
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

ViewModel

private ObservableCollection<Section> _surveyTreeModel;
public ObservableCollection<Section> SurveyTreeModel
{
    get { return _surveyTreeModel; }
    set
    {
        _surveyTreeModel = value;
        RaisePropertyChanged("SurveyTreeModel");
    }
}

private string _selectedQuestionId = null;
public string SelectedQuestionId
{
    get { return _selectedQuestionId; }
    set
    {
        _selectedQuestionId = value;
        RaisePropertyChanged("SelectedQuestionId");
    }
}

public RelayCommand<Question> SelectQuestionCommand { get; private set; }

public EditPageViewModel()
{
    SelectQuestionCommand = new RelayCommand<Question>(SelectQuestion);
}

private void SelectQuestion(Question obj)
{
    SelectedQuestionId = obj.QuestionId;
}

Outcome image

After I click the first edit button image

After I click the second edit button, the first item should revert back to preview mode. image

pedrolamas commented 5 years ago

This is actually expected as the DataTriggerBehavior executes an action when the evaluated condition goes true, but it does not revert back to the previous value if the condition goes false; that will be up to you to add a second DataTriggerBehavior with the false evaluation action.