我的ObservableCollection<Point>
显示为ItemsControl
,我在XAML中设置了ItemsPanelTemplate
,ItemsPanel
和ItemTemplate
属性。
我希望我的点代表边界框内的标准化位置。所以,如果我有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>
答案 0 :(得分:0)
好吧,我使用了一个相对简单的(虽然一如既往的详细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();
}
}