2013-06-12 31 views
6

我遇到了一個奇怪的問題。我在UserControl內顯示一個UserControl,此刻每個包含一個完全相同的ListView,如同我將它複製並粘貼到另一個。在父UserControl上,ListView顯示正確,但在子UserControl(不管它是什麼)上,ListView列拒絕伸展以適應其內容。我不知道爲什麼會發生這種情況,因爲ListView s的代碼完全一樣。兩者都直接放在電網上。完全相同的ListViews顯示不同 - 列不能伸展在一個

enter image description here

,左上方是從父控制ListView和右下方是孩子的控制。下面是兩個ListView是代碼:

<ListView ItemsSource="{Binding Adventurers}" 
       Name="AdvListView" 
       HorizontalAlignment="Stretch" 
       Grid.Column="2" 
       Grid.ColumnSpan="1" 
       Grid.Row="2" 
       ScrollViewer.CanContentScroll="False" 
       BorderThickness="3"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseDoubleClick"> 
       <cmd:EventToCommand Command="{Binding ShowAdvWindowCommand}" 
            CommandParameter="{Binding ElementName=AdvListView, Path=SelectedItem}" 
            PassEventArgsToCommand="False" /> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
     <ListView.ItemContainerStyle> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.ItemContainerStyle> 
     <ListView.Resources> 
      <DataTemplate x:Key="NameTemplate"> 
       <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding Name}" VerticalAlignment="Center" TextTrimming="CharacterEllipsis" /> 
      </DataTemplate> 
      <DataTemplate x:Key="StatusTemplate"> 
       <TextBlock Margin="2,1,1,1" Text="{Binding Status}" VerticalAlignment="Center" /> 
      </DataTemplate> 
     </ListView.Resources> 
     <ListView.View> 
      <GridView> 
       <GridViewColumn Header="Name" CellTemplate="{StaticResource NameTemplate}" /> 
       <GridViewColumn Header="Status" CellTemplate="{StaticResource StatusTemplate}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 

爲什麼會在一個而不是其他工作的呢?我現在真的很茫然。

更新:在父UserControl,網格佈局是:

<Grid Background="White"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 
     //Listview here among other controls 
</Grid> 

在孩子:

<Grid> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
    </Grid.RowDefinitions> 
     //Listview here 
</Grid> 

修改Grid.RowGrid.ColumnRowSpan,或ColumnSpan是正確的沒有按」解決問題。

更新:更多信息可能相關:當我嘗試在我的程序中使用Snoop時。我得到這個錯誤:

「檢測到綁定失敗 - 顯示名稱爲'Snoop.XmlSerializers'的程序集未能加載到ID爲1的AppDomain的'LoadFrom'綁定上下文中。失敗的原因是:System。 IO.FileNotFoundException:無法加載文件或程序集「Snoop.XmlSerializers,Version = 2.8.0.0,Culture = neutral,PublicKeyToken = null」或其某個依賴項,系統找不到指定的文件。

Snoop在其他程序中沒有問題。我不知道這是否與這個問題有關,但我想我會發布它。

更新:我不認爲這會事,但只是要徹底,這裏是孩子UserControl在父內創建:

<Grid> 
    //Other controls 
    <UserControl Grid.Column="3" 
       Grid.Row="3" 
       Visibility="{Binding AdventurerInfoVisibility}"> 
     <view:AdventurerInfoView /> 
    </UserControl> 
</Grid> 

在這一點上,我完全難倒。我可以在父母UserControl中創建一個ListView的副本,並將其正確顯示......但由於某種原因,它不想在子女UserControl中表現出色......我願意接受任何想法,如果您需要更多信息代碼我會很樂意提供它。

+0

如果它們相同,你可以發佈包含listview的網格控件的xaml嗎? –

+0

我已更新我的帖子。 –

回答

0

我創建一個deom使用你的代碼,它不會重複你的問題。

我想也許你的孩子usercontrol有一些TextBlock樣式,並覆蓋你的容器設置。

+0

也許你可以使用Snoop來觀看列的寬度並從什麼方式設置。 – muzizongheng

+0

這當然很有趣。當我嘗試在我的程序中使用Snoop時,會發生這種情況:「檢測到BindingFailure - 顯示名稱爲'Snoop.XmlSerializers'的程序集無法加載到ID爲1的AppDomain的'LoadFrom'綁定上下文中。失敗的原因是:System.IO.FileNotFoundException:無法加載文件或程序集「Snoop.XmlSerializers,Version = 2.8.0.0,Culture = neutral,PublicKeyToken = null」或其某個依賴項,系統找不到指定的文件。儘管如此,Snoop在其他程序上工作得很好。 –

+0

此外,子UserControl沒有自己的樣式。所有那裏都有網格和列表視圖。 –

1

經過多次修補和挫折之後,我發現是什麼導致了這個問題。 UserControl的可見性子項默認設置爲摺疊狀態,並通過單擊按鈕顯示。當我默認可見時,列正確拉伸。

現在我只需要找出如何讓它開始摺疊並仍然保持拉伸列的寬度。

相關問題