0

我有一个ObservableCollection<Point>显示为ItemsControl,我在 XAML 中设置 和ItemsPanelTemplate属性的地方。ItemsPanelItemTemplate

我希望我的点代表边界框内的标准化位置。所以,如果我有一个Point(0.5, 0.5),它将在容器的正中间。现在如果是Point(0.25, 0.75),它将定位在容器宽度的 25% 和容器高度的 75% 处。

问题是:我如何在 WPF 中实现它?我应该在 ItemContainerStyle 绑定中放置一个 ValueConverter 吗?我应该使用 Behavior、DataTemplate、ElementBinding、MultiBinding...吗?

我有点失落...

XAML 代码,使用绝对定位(不是我想要的):

            <ItemsControl x:Name="MarcadoresSelecionadosZoom" ItemsSource="{Binding ListaPontos}" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="{x:Null}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <Canvas IsItemsHost="True" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>                      
                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="{x:Type FrameworkElement}">
                        <Setter Property="Canvas.Left" Value="{Binding X}" />
                        <Setter Property="Canvas.Top" Value="{Binding Y}" />
                    </Style>                
                </ItemsControl.ItemContainerStyle>
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="Point">
                        <Ellipse Fill="Blue"
                            Width="8"
                            Height="8"
                            Margin="-4,-4,4,4" />
                    </DataTemplate>             
                </ItemsControl.ItemTemplate>
            </ItemsControl>
4

1 回答 1

1

好吧,我让它使用了一个相对简单的(尽管与 XAML 一样冗长)解决方案,涉及 MultiBinding 和 MultiValueConverter。我改变了ItemContainerStyle单独,并在代码隐藏中创建了转换器:

XAML:

                <ItemsControl.ItemContainerStyle>
                    <Style TargetType="{x:Type FrameworkElement}">
                        <Setter Property="Canvas.Left">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource NormalConverter}">
                                    <Binding Path="X"/>
                                    <Binding ElementName="MarcadoresSelecionados" Path="ActualWidth"/>
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                        <Setter Property="Canvas.Top">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource NormalConverter}">
                                    <Binding Path="Y"/>
                                    <Binding ElementName="MarcadoresSelecionados" Path="ActualHeight"/>
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                    </Style>                
                </ItemsControl.ItemContainerStyle>

转换器:

public class NormalConverter : IMultiValueConverter
{
    public object Convert(object[] values,
                          System.Type targetType,
                          object parameter,
                          System.Globalization.CultureInfo culture)
    {
        double menor = (double)values[0];
        double maior = (double)values[1];
        return maior * menor;
    }

    public object[] ConvertBack(object value, System.Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new System.NotImplementedException();
    }
}
于 2013-11-06T19:06:29.343 回答