简体   繁体   English

WPF ItemsControl Datattemplate动态更新

[英]WPF ItemsControl Datattemplate dynamic update

I'm new in WPF technology and I would like to ask about possibility of solving of my problem. 我是WPF技术的新手,我想问一下解决我的问题的可能性。

I have ItemsControl where I add images: 我在添加图像的地方有ItemsControl:

<ItemsControl  Name="itemscontrol1" 
    ItemsSource="{Binding Path=PicturesList}" ItemTemplate="{StaticResource pictureTemplate}" Grid.Column="1" />

And then I have prepare Datatemplate: 然后我准备了Datatemplate:

        <DataTemplate x:Key="pictureTemplate"> 

        <DataTemplate.Resources>

            <Style TargetType="Image">

                <Setter Property="Width" Value="180" />

                <Setter Property="Height" Value="120" />

                <Setter Property="Margin" Value="10" />

            </Style>
        </DataTemplate.Resources>

        <Image Source="{Binding Path=Location}" />
    </DataTemplate>

My problem is that I don't know how I can dynamically change parametrs of image width and height. 我的问题是我不知道如何动态更改图像宽度和高度的参数。 For example, when I change size of window, i need change size of images inside item control. 例如,当我更改窗口的大小时,我需要在项目控件中更改图像的大小。

Could someone help me? 有人可以帮我吗?

Thank you for advice and tips. 感谢您的建议和提示。 Smith 史密斯

If your controlling the size of the image based on the rest of the UI, ie Window size you should avoid setting them to explicit values and opt for a layout panel which takes care of this for you. 如果您基于UI的其余部分控制图像的大小(即窗口大小),则应避免将它们设置为显式值,而应选择一个布局面板来为您解决此问题。 Such as making the controls items panel a wrappanel layout. 例如使控件项目面板成为包装面板布局。

  <ItemsPanelTemplate x:key="griditemtemplate">
    <WrapPanel VerticalAlignment="Center"
          HorizontalAlignment="Center"/>
  </ItemsPanelTemplate>

If there is something in your view model that dictates the size, use a binding. 如果您的视图模型中有规定大小的东西,请使用绑定。

To do what you want specifically, you would need a uniform grid and be able to bind the number of columns to collection count. 要具体执行您想要的操作,您将需要一个统一的网格并能够将列数绑定到集合数。 Unfortunately this can't be done to my knowledge. 不幸的是,这据我所知无法完成。

As such, I'd create a custom layout panel. 因此,我将创建一个自定义布局面板。 Lots of tutorials available online , but basically create a class that extends Panel and then override MeasureOverride and ArrangeOverride methods. 许多教程可在线获得 ,但基本上是创建一个扩展Panel的类,然后重写MeasureOverride和ArrangeOverride方法。 The important one being Measure... 重要的是措施...

protected override Size ArrangeOverride(Size finalSize)
{
    int i = 0;
    foreach( UIElement child in InternalChildern)
    {
        var r = new Rect(this.ActualSize / this.InternalChildren.count * i, 0, this.ActualSize / this.InteralChildren.Count, finalSize.Height);
        child.Arrange( r );
        i++;
    }
}

This is approximate code written on my phone so apologies if needs refactoring. 这是写在我手机上的近似代码,因此如果需要重构很抱歉。

Then use this panel as your ItemsPanel template as shown before. 然后,如前所示,将该面板用作ItemsPanel模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM