Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 绑定不提交?_Wpf_Binding - Fatal编程技术网

Wpf 绑定不提交?

Wpf 绑定不提交?,wpf,binding,Wpf,Binding,我有一个TabControl绑定到ICollectionView,其中派生自observedcollection。我认为相当标准的MVVM多文档模式?无论如何,EditorTabViewModel有一个属性Content,其中包含要显示的字符串。我发现绑定正在工作 // Add 2 default tabs for a test, also set their Content property to the respective values ... _tabs.Add(new EditorTa

我有一个
TabControl
绑定到
ICollectionView
,其中派生自
observedcollection
。我认为相当标准的MVVM多文档模式?无论如何,
EditorTabViewModel
有一个属性
Content
,其中包含要显示的字符串。我发现绑定正在工作

// 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>

结果

但当我更改值、切换制表符并返回时,我会再次在构造函数中设置字符串。。。显示在


我假设
MarkdownEditor.TextContent
属性不会告诉任何人它的值已更改,因此绑定机制不会费心将它的新值写入
EditorTabViewModel.Content
。如果
TextContent
MarkdownEditor
的依赖属性,您能否确保它从用于实际编辑文本的控件(
TextBox
或其他内容)接收更改的文本?

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


Mediafire不允许我下载源代码。大概是因为我所在地区的很多人正在尝试从中下载一些东西。虽然我不确定,但这种解决方案可能会导致内存消耗过高。您将在
txtEditor
中的每次文本更改中分配一个新字符串。对于短文本来说,它是可以负担的,但对于文档编辑器来说则不然。最好挂接LostFocus事件或其他内容。@alpha鼠标由于.NET垃圾收集器的工作方式,创建大量(大量)短期对象(如字符串)很少是问题。@Bevan:我们在这里讨论的是文本编辑器。所以这些字符串不会特别小。@alpha-mouse:我想你一开始就误解了这个问题。
TextBox
的文本属性的默认
UpdateSourceTrigger
LostFocus
。但是,在直接更改选项卡时从未引发该事件,因此从未更新源。此外,我还尝试在该文本框中插入和处理大量文本,但我没有注意到
LostFocus
PropertyChanged
@alpha鼠标在性能或内存消耗方面有任何差异。从垃圾收集器的角度来看,“small”的阈值是一个相当大的字符串。87KB,如果内存可用的话。这是一篇相当大的文章。尽管如此,您还是提出了一个很好的观点——使用多兆字节字符串是一种产生大量内存压力的好方法。