我的页面上有一个滑块,拖动时也会增加列表框项目的大小。我怎样才能实现这一目标?如何在ItemTemplate中引用父容器,然后修改它的高度和宽度?目前我在滑块值更改事件上有此代码: -
void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
Border parentBorder = ((Border)lstAlbumPhotos.ItemTemplate.LoadContent());
double change = e.NewValue * 10;
double percentage = 100 + change;
double newWidth = percentage * _width / 100;
double newHeight = percentage * _height / 100;
parentBorder.Width = newWidth;
parentBorder.Height = newHeight;
}
但它不起作用。在上面的代码中,Border是我的父容器。
提前致谢:)
答案 0 :(得分:1)
LoadContent
方法创建模板中保存的Xaml的新实例。您无法以这种方式操纵模板本身的内容。最重要的是,我真的不认为你想这样做。
如果你确实想要操纵模板中边框的宽度和高度,那么使用一些绑定到作为静态资源的中间对象(我称之为“Sizer”): -
<Grid.Resources>
<local:Sizer x:Key="Sizer" />
</Grid.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<Border Width="{Binding Width, Source={StaticResource Sizer}}"
Height="{Binding Height, Source={StaticResource Sizer}}" />
您还可以将滑块绑定到此中介对象: -
<Slider Value="{Binding Factor, Mode=TwoWay, Source={StaticResource Sizer}}" />
现在,您只需要创建一个具有Sizer
,Factor
,Width
属性的Height
类。您可以实现INotifyPropertyChanged,以便更新属性上的绑定。然后,将数学移动到此对象中。当更改因子时,您可以更改“宽度”和“高度”属性,并让绑定处理更新所有现有边框。