Binding not fixed?

I have a TabControlrelated ICollectionViewone that matters ObservableCollection<EditorTabViewModel>. I think the standard MVVM Multi-Document template? In any case, it EditorTabViewModelhas a property Contentthat contains a string to display. I found that binding works ...

// Add 2 default tabs for a test, also set their Content property to the respective values ...
_tabs.Add(new EditorTabViewModel { Content = "Tab 1" });
_tabs.Add(new EditorTabViewModel { Content = "Tab 2" });

Its values ​​are displayed correctly

Xaml

<!-- DataTemplate to render EditorTabViewModels -->
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
    <me:MarkdownEditor 
        TextContent="{Binding Path=Content.Content, RelativeSource={RelativeSource Mode=TemplatedParent}, Mode=TwoWay}" 
        Options="{Binding Path=Options, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
</DataTemplate>

Result

But when I change the value, switch the tabs and return, I again get the line set in the constructor ... shown in this video (on screen)

Visual studio solution

+3
source share
2 answers

UpdateSourceTrigger PropertyChanged TextBox "txtEditor" MarkdownEditor.xaml. UpdateSourceTrigger TextBox - LostFocus, .

<TextBox Grid.Row="1" x:Name="txtEditor" AcceptsReturn="True"
         Text="{Binding TextContent, UpdateSourceTrigger=PropertyChanged}" 
         FontFamily="{Binding Path=Options.FontFamily}"
         FontSize="{Binding Path=Options.FontSize}"
         FontWeight="{Binding Path=Options.FontWeight}"
         Background="{Binding Path=Options.Background}"
         Foreground="{Binding Path=Options.Foreground}" />
0

, MarkdownEditor.TextContent , , EditorTabViewModel.Content. TextContent MarkdownEditor, , , (TextBox - )?

0

Source: https://habr.com/ru/post/1775977/


All Articles