Search code examples
wpfdatatemplateresize

WPF : Resize DataTemplate


I tried to set width and height property to my data template control but my control keeps its default size.

<ItemsControl Grid.Row="1" x:Name="containerUsers" ItemsSource="{Binding ValidUsers}" >
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel IsItemsHost="True"  AllowDrop="True" ClipToBounds="False" DragEnter="panelUsers_DragEnter" Drop="panelUsers_Drop" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <my:PictureLabelControl Width="20" Height="50" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

In fact I have three panels like these and I wish I could change the size of the control when it comes to another panel by drag n drop.

<UserControl x:Class="VHTService.Wfm.View.PictureLabelControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" Cursor="Hand" Focusable="True" Margin="3">
<Grid>
    <Grid Name="grid1">
        <Label Content="{Binding LabelText}" Focusable="True" FontSize="12" FontWeight="Normal" HorizontalContentAlignment="Center" 
               Name="lblPicture" VerticalContentAlignment="Center" Height="26" VerticalAlignment="Bottom" />
        <Image Source="{Binding Picture}" Focusable="True" Name="imgAvatar" Stretch="Uniform" Margin="0,0,0,26" GotFocus="imgAvatar_GotFocus" 
               LostFocus="imgAvatar_LostFocus" />
    </Grid>
</Grid>


Solution

  • I made the following example window and the items respect the size given. Could you post the PictureLabelControl? I think the error is somewhere in there.

    <Window
        x:Class="Project1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow"
        Height="350"
        Width="525">
        <ItemsControl>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel
                        IsItemsHost="True"
                        AllowDrop="True"
                        ClipToBounds="False"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock
                        Background="Gray"
                        Text="{Binding}"
                        Width="50"
                        Height="50"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.Items>
                <System:String>foo1</System:String>
                <System:String>foo2</System:String>
            </ItemsControl.Items>
        </ItemsControl>
    </Window>