简体   繁体   中英

Multibinding not working - StrokeThickness of paths is unchanged

I have an ItemsControl with ItemsSource filled by an ObservableCollection of Paths. The Path class that implements INotifyPropertyChanged has a property named StrokeThickness :

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

In our ViewModel we have :

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

And this is my View :

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

I have used a Multibinding Convertor to give me true StrokeThickness based on ZoomRatio. ZoomRatio is being calculated every time map zoomed.

This is my MultiBinding converter :

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();
    }
}

when I trace this Converter its work fine but every time it's entry data for StrokeThikness is same and it means return value doesn't change StrokeThikness of Paths.

Am I doing something to wrong?

The first Binding in the MultiBinding is wrong. It should look like this:

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

Also check if the property name is correct, as you constantly write StrokeThikness instead of StrokeThickness in your question.

You should also check your converter code. It seems you are dividing ZoomRatio by StrokeThickness , which to my understanding should be the other way round.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM