0

我试图为我的数据模板控件设置宽度和高度属性,但我的控件保持其默认大小。

<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>

事实上,我有三个这样的面板,我希望当涉及到另一个面板时,我可以通过拖放来更改控件的大小。

<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>

4

1 回答 1

1

我制作了以下示例窗口,并且项目尊重给定的大小。你能发布 PictureLabelControl 吗?我认为错误就在那里。

<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>
于 2012-06-27T15:26:48.370 回答