开发者

How can i get reference to ItemTemplate's root container and then modify it's properties?

I have a slider on my page which when dragged also should increase the size of the listbox items. How can i achieve this? How can i get reference to the parent container in ItemTemplate and then modify it's height and width? Currently i have this code on my slider value changed event :-

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 * _wid开发者_运维百科th / 100;
            double newHeight = percentage * _height / 100;
            parentBorder.Width = newWidth;
            parentBorder.Height = newHeight;

        }

But it is not working. In the above code Border is my parent container.

Thanks in advance :)


The LoadContent method creates a new instance of the Xaml held in the template. You can not manipulate the content of the template itself in this way. On top of that I really don't think you want to be doing that.

If you really do want to manipulate the width and height of the border in the template then use some binding to an intermediatory object (I'll call it "Sizer") held as a static resource:-

<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}}" />

You would also bind your slider to this intermediatory object:-

 <Slider Value="{Binding Factor, Mode=TwoWay, Source={StaticResource Sizer}}" />

Now you simply have to create a Sizer class that has the properties Factor, Width, Height. You would have it implement INotifyPropertyChanged so that the binding on the properties will be be updated. You then move your math into this object. When Factor is changed you change the Width and Height properties and let the bindings handle update all the existing Borders.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜