簡體   English   中英

未調用 ConvertBack

[英]ConvertBack is not called

我正在嘗試編寫一個通用控件,我可以在其中使用轉換器傳遞不同的數據模型(都實現INotifyPropertyChanged )。 數據在控件中沒有問題地傳遞並正確顯示(其中一些還使用IMultivalueConverts ,它可以完美地工作)。 雖然修改了數據,但是沒有調用MainControl的IMultiValueConverter

通用控件應該只顯示根據 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作為綁定添加到 Big Converter 時,會觸發更新,但所有綁定都會丟失。

對原始代碼的更改是:將所有內部變量更改為DependencyProperties ,這使得ObservableCollection的單個條目的更新可靠。

GUI 和使用的轉換器概述

是時候自己回答這個問題了:

當兩個元素之間存在直接連接時,我假設變化通過轉換器向下傳播是錯誤的。

因此,如果我在深度嵌套中有一個元素(僅將其視為樹的葉子),並且它曾經通過轉換器(它更改一些其他對象)顯示一次,並且同時沒有轉換器,則該元素將在不更新的情況下進行更新調用轉換器。 WPF 非常聰明地看到該值是相同的,並且不會通過轉換器上下傳輸變化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM