簡體   English   中英

多重綁定不起作用-路徑的StrokeThickness不變

[英]Multibinding not working - StrokeThickness of paths is unchanged

我有一個ItemsControl其中ItemsSource由Paths的ObservableCollection填充。 實現INotifyPropertyChanged的Path類具有一個名為StrokeThickness的屬性:

private double _strokeThickness;
public double StrokeThickness
{
    get { return _strokeThickness; }
    set
    {
        _strokeThickness = value;
        OnPropertyChanged(nameof(StrokeThickness));
    }
}

在我們的ViewModel中,我們有:

public ObservableCollection<Path> PathCollection
{
    get { return _pathCollection; }
    set
    {
        _pathCollection = value;
        OnPropertyChanged(nameof(PathCollection));
    }
}

這是我的觀點:

<!-- Paths -->
<ItemsControl ItemsSource="{Binding PathCollection}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Path Stroke="{Binding Stroke}"
                  Data="{Binding Data}">
                <Path.StrokeThickness>
                    <MultiBinding Converter="{StaticResource    
                        CorrectStrockThiknessConvertor}">
                        <MultiBinding.Bindings>
                            <Binding Source="{Binding
                                StrokeThickness}"></Binding>
                            <Binding ElementName="RootLayout" 
                                Path="DataContext.ZoomRatio" >
                            </Binding>
                        </MultiBinding.Bindings>
                    </MultiBinding>
                </Path.StrokeThickness>
            </Path>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Canvas x:Name="Canvas"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

我使用了多綁定轉換器,根據ZoomRatio給我真正的StrokeThickness。 每次地圖縮放時都會計算ZoomRatio。

這是我的MultiBinding轉換器:

public class CorrectStrockThiknessConvertor : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values != null & values.Length == 2 && (double)values[0] != 0)
        {
            return (double)values[1] / (double)values[0];
        }
        return 1;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

當我跟蹤此Converter時,它的工作正常,但每次它的StrokeThikness條目數據都是相同的,這意味着返回值不會更改Path的StrokeThikness。

我在做錯什么嗎?

MultiBinding中的第一個Binding錯誤。 它看起來應該像這樣:

<MultiBinding.Bindings>
    <Binding Path="StrokeThickness" />
    <Binding ElementName="RootLayout" Path="DataContext.ZoomRatio" />
</MultiBinding.Bindings>

還要檢查屬性名稱是否正確,因為您在問題中不斷寫入StrokeThikness而不是StrokeThickness

您還應該檢查您的轉換器代碼。 看來您正在將ZoomRatio除以ZoomRatioStrokeThickness我了解,應該相反。

暫無
暫無

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

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