[英]Binding the control's property in its DataTemplate
I have a custom control where I have modified a ListView. 我有一个自定义控件,我修改了ListView。 I have a DataTemplate which shows each item as an icon of width 128 and a label beneath it.
我有一个DataTemplate,它将每个项目显示为宽度为128的图标,并在其下方显示一个标签。
<DataTemplate x:Key="AeroIconTemplate">
<Grid VerticalAlignment="Top" Margin="0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Image Grid.Row="0" Source="{Binding Image}" Width="128"
MaxHeight="128" Margin="0"/>
<TextBlock Grid.Row="1" Text="{Binding Title}" Foreground="Black"
TextWrapping="WrapWithOverflow" Margin="0"
TextTrimming="CharacterEllipsis" Width="128" MaxHeight="60" />
</Grid>
</DataTemplate>
Now I have added a property to the ListView itself called IconSize. 现在我已经向ListView本身添加了一个名为IconSize的属性。 This takes an integer between 16 and 256.
这需要16到256之间的整数。
I want to bind the Width
, MaxHeight
of the Image
and the Width
of the TextBlock
to this property. 我想将绑定
Width
, MaxHeight
中的Image
和Width
的的TextBlock
这个属性。 So whenever the IconSize property is changed the template is adjusted in size. 因此,每当更改IconSize属性时,都会调整模板的大小。 As you can see I am currently binding some stuff to the data object (the image source and the label text), but in this case I want to bind to the ListView control .
正如您所看到的,我目前正在将一些内容绑定到数据对象 (图像源和标签文本),但在这种情况下,我想绑定到ListView 控件 。
How do I do this? 我该怎么做呢?
Thanks! 谢谢!
You can use the RelativeSource
for that: 您可以使用
RelativeSource
:
... Width="{Binding IconSize,RelativeSource={RelativeSource AncestorType=ListView}}" ...
Be sure to define the correct type, since the WPF ListView
does not have the property IconSize
. 请务必定义正确的类型,因为WPF
ListView
没有属性IconSize
。 You may need to define your class. 您可能需要定义您的课程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.