2016-11-18 89 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这不回答这个问题,我也不会接受它作为回答希望得到强有力的答案。