2011-05-17 78 views
0

我試圖用項目填充列表框。該ItemsSource如下:WPF .NET4.0列表框和ItemsControl

public SortedDictionary<string, List<int>> AvailableValues 
時,我有以下的項目

似乎很好地奠定了。除了我不能選擇一個整體項目,並執行一些功能。

<ScrollViewer Grid.Row="0" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled" > 
     <ItemsControl ItemsSource="{Binding AvailableValues}"> 

      <ItemsControl.Template> 
       <ControlTemplate> 
        <CustomControls:UniformWrapPanel IsItemsHost="True"/> 
       </ControlTemplate> 
      </ItemsControl.Template> 

      <ItemsControl.ItemTemplate> 
       <DataTemplate> 

        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition Height="*"/> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="*"/> 
          <ColumnDefinition Width="60"/> 
         </Grid.ColumnDefinitions> 


         <Label Grid.Row="0" Grid.Column="0" Content="{Binding Key}" /> 
         <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Value}" SelectedItem="{Binding SelectedInputValue, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True" /> 

        </Grid> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 

伊夫試圖替換ItemsControlListBox(以及ListBox.ItemTemplate)和不能似乎得到其中由LabelContent是左對齊的顯示和Combobox內容是正確的對齊。

UniformWrapPanel來自CodeProject文章。

感謝,

回答

1

如果對齊是主要問題,則可能需要嘗試將標籤和組合框包裝到網格。將網格的水平對齊設置爲拉伸,並將對齊的水平對齊設置爲左側。

設置一個最小最大尺寸是可選的,但可能是有用的

<Grid HorizontalAlignment="Stretch"> 
      <Grid.ColumnDefinitions> 
      <ColumnDefinition MinWidth="55" /> 
      <ColumnDefinition Width="0.636*" /> 
      </Grid.ColumnDefinitions> 
      <Label Grid.Column="0" HorizontalALignment="Left"/> 
      <ComboBox Grid.Column="1" HorizontalAlignment="Left"/> 
     </Grid> 
+0

Thanks @randyc。奇怪的是,即使使用上面的定義,它的情節也是如此,但是在'Oriention'設置爲'Horizo​​ntal'的情況下,兩個項目包含在一個StackPanel中。然而,上面的定義有訣竅! – TheRenoRanger 2011-05-23 02:23:44

1

ItemsControl是不一樣的ListBox - 它不包含選擇的能力。

最好的辦法是使用實​​際的ListBox並修改ItemTemplate以顯示您想要的內容。

<ListBox x:Name="MyListBox" ItemsSource="{Binding AvailableValues}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid HorizontalAlignment="Stretch"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="*"/> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="*"/> 
        <ColumnDefinition Width="60"/> 
       </Grid.ColumnDefinitions> 

       <Label Grid.Row="0" Grid.Column="0" Content="{Binding Key}" /> 
       <ComboBox Grid.Row="0" Grid.Column="1" ItemsSource="{Binding Value}" /> 

      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

乾杯@Rachel。我在「ItemsPanel」下花了很多時間,即使是Canvas設置。最後@ randyc的建議做到了。 – TheRenoRanger 2011-05-23 02:25:35