2012-06-27 168 views
0

我試着將寬度和高度屬性設置爲我的數據模板控件,但我的控件保持其默認大小。WPF:調整DataTemplate的大小

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

其實我有三個面板這樣的,我希望當它通過拖放的N個液滴談到另一個面板,我可以改變控件的大小。

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

回答

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

我只是試圖把默認大小到我的用戶控制和現在,它需要的面板容器的大小 – Akhilleus

+0

嘗試在用戶控件內部的控件伸展設置的Horizo​​ntalAlignment和VerticalAlignment。這應該使他們填滿整個模板。 – Mizipzor

+0

事實上,我不想伸展我的'UserControl',這就是它現在的樣子。我想要像你的例子那樣很好地處理'TextBlock',但不能用我的'PictureLabelControl' – Akhilleus