c# - 未调用 ConvertBack
问题描述
我正在尝试编写一个通用控件,在该控件中,我可以INotifyPropertyChanged
使用转换器传递不同的数据模型(全部实现)。数据在控件中传递没有问题并正确显示(其中一些还使用IMultivalueConverts
,它完美地工作)。虽然修改了数据,但IMultiValueConverter
没有调用 MainControl 的。
通用控件应该只显示从 ObservableCollection 中的坐标计算的矩形。
我将 DebugConverters 放在所有绑定上,除了 ConvertBack 到顶部之外,所有内容似乎都已更新。ListBox 中的 SourceUpdate 也被调用。
我用不同的 NotifyOn...Updated、Mode 和 UpdateSourceTrigger 尝试了这个转换器,我总是看到控件中的值发生变化,但从来没有看到主控件的 ConvertBack。
使用的部分数据模板(更新正确完成),所有 DegreeTo... 转换器都被称为双向
<DataTemplate x:Key="RectangleWithLabel">
<Canvas IsHitTestVisible="True">
<Rectangle x:Name="RectangleROI" MouseLeftButtonDown="myCanvas_PreviewMouseLeftButtonDown" >
<!--
<Rectangle.Visibility>
<Binding Path="ROI" Converter="{StaticResource NullToVisibilityConverter}"/>
</Rectangle.Visibility>
-->
<Canvas.Left>
<MultiBinding Converter="{StaticResource DegreeToScreenPixelConverterH}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" >
<Binding Path="ROI.Begin.PosH" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" />
<Binding Path="DataContext.UsedCoordinateSystem" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" NotifyOnSourceUpdated="True" Mode="TwoWay" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" />
</MultiBinding>
</Canvas.Left>
<Canvas.Top>
<MultiBinding Converter="{StaticResource DegreeToScreenPixelConverterV}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="ROI.Begin.PosV" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" />
<Binding Path="DataContext.UsedCoordinateSystem" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" NotifyOnSourceUpdated="True" Mode="TwoWay" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</Canvas.Top>
<Rectangle.Width>
<MultiBinding Converter="{StaticResource DegreeToScreenPixelWidthConverter}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="ROI.Begin.PosH" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" />
<Binding Path="ROI.End.PosH" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" />
<Binding Path="DataContext.UsedCoordinateSystem" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" NotifyOnSourceUpdated="True" Mode="TwoWay" NotifyOnTargetUpdated="True"/>
</MultiBinding>
</Rectangle.Width>
<Rectangle.Height>
<MultiBinding Converter="{StaticResource DegreeToScreenPixelHeightConverter}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="ROI.Begin.PosV" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True"/>
<Binding Path="ROI.End.PosV" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" />
<Binding Path="DataContext.UsedCoordinateSystem" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" NotifyOnSourceUpdated="True" Mode="TwoWay" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</Rectangle.Height>
<Rectangle.Fill>#33FF0000</Rectangle.Fill>
<Rectangle.Stroke>#FF00FF00</Rectangle.Stroke>
<Rectangle.IsHitTestVisible>true</Rectangle.IsHitTestVisible>
</Rectangle>
包含所有数据的列表视图:
<ListView ItemsSource="{Binding Rectangles, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" x:Name="listBox" Width="{Binding ActualWidth, ElementName=ImageControl, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Height="{Binding ActualHeight, ElementName=ImageControl, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}" ItemTemplate="{StaticResource RectangleWithLabel}" MouseMove="ListBox_MouseMove" DataContextChanged="ListBox_DataContextChanged" SourceUpdated="ListBox_SourceUpdated" IsSynchronizedWithCurrentItem="True" TargetUpdated="ListBox_TargetUpdated" />
从父级调用 ImageViewer,这里转换器在到 ImageViewer 的途中被调用,但从未调用 ConvertBack:
<common:ImageViewer x:Name="ctrlImage" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<common:ImageViewer.DataContext>
<MultiBinding Converter="{StaticResource ConverterWhichIsOnlyCalledOneWay}" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged">
<Binding Path="." UpdateSourceTrigger="PropertyChanged" Mode="TwoWay" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True"/>
</MultiBinding>
</common:ImageViewer.DataContext>
</common:ImageViewer>
我怎样才能得到ConvertBack
被调用?
编辑:
我对控件和转换器进行了概述。我想我错了,应该调用 ConvertBack,一切都在更新,即使没有大转换器。但是在向ObservableCollection
. 当我添加一些值时,这些值出现在 MainWindow 的 DataContext 中。但是转换器没有被触发。当我将Observable.Count
as Binding 添加到 Big Converter 时,会触发更新,但所有绑定都会丢失。
对原始代码的更改是: 将所有内部变量更改为DependencyProperties
,这使得更新对ObservableCollection
.
解决方案
是时候自己回答这个问题了:
当两个元素之间存在直接连接时,假设更改通过转换器向下传播,我错了。
因此,如果我在深度嵌套中有一个元素(只是将其视为树的叶子),并且它曾经通过转换器显示一次(它会更改一些其他对象)并且同时没有转换器,则元素会在没有转换器的情况下更新调用转换器。WPF 非常聪明地看到值是相同的,并且不会通过转换器上下管道更改。
推荐阅读
- c++ - 具有统一初始化语法的自动类型推导 c++11 vs c++17
- html - 输入焦点时 CSS 过渡的方向发生变化
- laravel - 如何查看完整的 Laravel Mix 通知?
- azure-devops - 存储库中的 Azure DevOps markdown 预览:标题锚呈现为工作项链接
- couchdb - 在 couchDB 中制作多个单独的键
- python - 优化 MLP 回归分数
- php - 执行一段时间后如何中断 PHP 中的函数?
- css - 响应式移动 CSS
- css - 在 CSS 中,有没有办法在“内容”中的两个字符串之间添加制表符?
- c# - Blazor 组件未执行 NavManager.NavigateTo