gpt4 book ai didi

wpf - 绑定(bind)不提交?

转载 作者:行者123 更新时间:2023-12-04 23:53:25 26 4
gpt4 key购买 nike

我有一个 TabControl绑定(bind)到 ICollectionView with 派生自 ObservableCollection<EditorTabViewModel> .我认为非常标准的 MVVM 多文档模式?无论如何,EditorTabViewModel有一个属性Content包含要显示的字符串。我发现绑定(bind)正在工作......

// 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" });

它的值被正确呈现

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>

结果

但是当我更改值、切换选项卡并返回时,我再次在构造函数中设置了字符串...如 this video (on screenr) 所示

Visual Studio Solution

最佳答案

在 MarkdownEditor.xaml 中将文本框“txtEditor”的 UpdateSourceTrigger 更改为 PropertyChanged。 TextBox 的默认 UpdateSourceTrigger 值为 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}" />

关于wpf - 绑定(bind)不提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233193/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com