2016-11-18 88 views
0

我需要爲ListView中的每個項目水平顯示UserControl。它可以用StackPanel完成,但據我所知StackPanel將包裝,直到內容可以放置。但是我需要填充父項(在此示例中爲ListView),比如它的Grid與多個ListBoxes。順便說一句,我UserControl是這樣的:水平ListView內部的UserControl無法垂直填充父項

<UserControl x:Class="ResourceEditor.LanguagePanel" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:resources="clr-namespace:ResourceEditor.Resources"    
     xmlns:viewModel="clr-namespace:ResourceEditor.ViewModel"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"></RowDefinition> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 
    <DockPanel Grid.Row="1" LastChildFill="True" DockPanel.Dock="Bottom"> 
     <CheckBox HorizontalAlignment="Center" VerticalAlignment="Center" DockPanel.Dock="Right" IsChecked="{Binding SortByThis}"></CheckBox> 
     <UniformGrid Columns="2" Rows="0"> 
      <Button Content="{x:Static resources:Strings.Save}" DockPanel.Dock="Left" Command="{Binding SaveCommand}"></Button> 
      <Button Content="{x:Static resources:Strings.Reset}" DockPanel.Dock="Left" Command="{Binding ResetCommand}"></Button> 
     </UniformGrid> 

    </DockPanel> 


     <ListBox Grid.Row="0" SelectedItem="{Binding SelectedWord}" ItemsSource="{Binding Words}"> 
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="MouseDoubleClick"> 
        <command:EventToCommand Command="{Binding Mode=OneTime, Path=EnableEditingCommand}" 
          PassEventArgsToCommand="False" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
      <ListBox.ItemTemplate> 
       <HierarchicalDataTemplate DataType="viewModel:WordNodeVM"> 
       <TextBox IsEnabled="{Binding DataContext.CanEdit, RelativeSource={RelativeSource AncestorType=UserControl}}" Text="{Binding Name}"></TextBox> 
       </HierarchicalDataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
</Grid> 

這是我如何使用它:

<ListView Grid.Row="2" ItemsSource="{Binding Languages}"> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="Focusable" Value="false"/> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Rows="1" Columns="{Binding Languages.Count}"> 

       </UniformGrid> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
     <ListView.ItemTemplate> 
      <HierarchicalDataTemplate DataType="{ x:Type viewModel:LanguagePanelViewModel}"> 
       <resourceEditor:LanguagePanel DataContext="{Binding}"></resourceEditor:LanguagePanel> 
      </HierarchicalDataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

我嘗試了所有可能的解決方案,轉換容器,使用比對,比對內容,但無法讓我的用戶控件填充垂直空間。首先,我認爲它與UserControl有什麼不對,因爲它的行爲可能類似於StackPanel,但它不是。

+0

WPF有一個名爲WrapPanel的控件,那是你在找什麼? –

+0

我需要垂直填寫父項。但'WrapPanel'甚至比'StackPanel'更糟糕,因爲它包含了用戶控制的最小尺寸。 – Javidan

回答

0

我無法找到當前問題的解決方案。但就我想要的行爲而言,我通過這種方式實現了這一點: 我從佈局中刪除了ListView。添加空Grid。而在後臺代碼,添加此

public MainWindow() 
    { 
     var model=new MainViewModel(loadGrid); 
     DataContext = model; 
     InitializeComponent(); 
     Closing += (s, e) => ViewModelLocator.Cleanup(); 

    } 

    void loadGrid(IList<LanguagePanelViewModel> panels) 
    { 

     if (panels == null || !panels.Any()) 
      return; 

     for (int i = 0; i < panels.Count; i++) 
     { 
      mainGrid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star)}); 
      var panel = new LanguagePanel { DataContext = panels[i] }; 
      Grid.SetColumn(panel, i); 
      mainGrid.Children.Add(panel); 
     } 
    } 

我想不會打破MVVM規則,這loadGrid方法被調用時,我按照名單changes.As這不回答這個問題,我也不會接受它作爲回答希望得到強有力的答案。