如何在带有ItemContainerStyle的ListView中使用DisplayMemberPath?

时间:2017-09-07 09:07:40

标签: wpf xaml listview data-binding wpf-style

from math import cos, pi, sin from random import random from matplotlib.mlab import frange from matplotlib.pyplot import plot, axis, show,axes LEN = 0.1 def rotate(point, theta): x = point[0] y = point[1] x_ = x * cos(theta) + y * sin(theta) y_ = - x * sin(theta) + y * cos(theta) return x_, y_ class CUBE(object): def __init__(self, x, y, theta): self.corner = [(LEN / 2, LEN / 2), (-LEN / 2, LEN / 2), (-LEN / 2, -LEN / 2), (LEN / 2, -LEN / 2) ] self.theta = theta self.x = x self.y = y for i in range(4): self.corner[i] = rotate(self.corner[i], theta) self.corner[i] = (self.corner[i][0] + x, self.corner[i][1] + y) def is_include(cube, point): point = [point[0] - cube.x, point[1] - cube.y] point = rotate(point, -cube.theta) if (point[0] < -LEN / 2 or point[0] > LEN / 2 or point[1] < -LEN / 2 or point[1] > LEN / 2 ): return False else: return True def is_intersect(cube1, cube2): if (any([is_include(cube1, point) for point in cube2.corner]) or any([is_include(cube2, point) for point in cube1.corner]) or is_include(cube1, (cube2.x, cube2.y))): return True else: return False def plot_cube(cube,n): plot( [cube.corner[i][0] for i in [0, 1, 2, 3, 0]], [cube.corner[i][1] for i in [0, 1, 2, 3, 0]]) ax = axes() ax.text(cube.x,cube.y,str(n)) def display(cubelist): # connects the 4 corners on a plot for i,cube in enumerate(cubelist): plot_cube(cube,i) axis([0, 1, 0, 1]) # 1x1 grid show() cubelist = [] for i in range(100): x0 = random() y0 = random() theta = random() * pi cube = CUBE(x0, y0, theta) if any(is_intersect(cube,cb) for cb in cubelist): continue else: cubelist.append(cube) display(cubelist) 控件中,当我想要更改ListView时,如何使用DisplayMemberPath属性?

我在我的控件的ItemContainerStyle中使用了ListView控件,ControlTemplate属性是通过DisplayMemberPath从控件之外设置的。

Binding

是否可以在 XAML 中解决此问题?它是如何在原<ListView ItemsSource="{TemplateBinding ItemsSource}" DisplayMemberPath="{TemplateBinding DisplayMemberPath}"> <ListView.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Horizontal" /> </ItemsPanelTemplate> </ListView.ItemsPanel> <ListView.ItemContainerStyle> <Style TargetType="{x:Type ListViewItem}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <StackPanel Orientation="Horizontal"> <TextBlock Text="Item:" /> <TextBlock Text="{Binding}" /> <TextBlock Text=", " /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListView.ItemContainerStyle> </ListView> 中解决的?

我尝试用ControlTemplate来解决它,我绑定了Collection Item和MultiValueConverter

DisplayMemberPath

有必要吗?或者它是原始<TextBlock> <TextBlock.Text> <MultiBinding Converter="{StaticResource NameToValueConverter}"> <Binding /> <Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ListView}}"/> </MultiBinding> </TextBlock.Text> </TextBlock> 控件已解决的显示值?

1 个答案:

答案 0 :(得分:2)

  

是否有可能通过XAML解决?

不,您使用 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg viewbox="0 0 1600 1600" width="500" height="500"> <g id ='aa' onclick="rotate()"> <rect x="100", y="100" width="100" height="100" /> <circle cx="250" cy="150" r="50"/> </g> <g class ='aa' onclick="rotate2()"> <rect x="600", y="100" width="100" height="100" /> <circle cx="750" cy="150" r="50" /> </g> </svg>DisplayMemberPath / ItemTemplate,但不能同时使用两者。

此外,ItemContainerStyle属性应设置为DisplayMemberPath,指定基础数据类的属性名称。 是一个可以绑定的依赖项属性。