1

在我的一個應用程序中,我有一個名爲FeedControl的用戶控件(它是另一個RSS閱讀器!),它非常簡單 - 只需一個複選框來選擇項目,顯示一個文本塊供稿名稱和另一個文本塊來顯示未讀供稿項目的數量。在啓動時將這些提要加載到可觀察的集合中,然後爲每個提要創建一個控件實例,並將它們全部添加到列表框中 - 這非常簡單。WP7 usercontrol - 改變方向變化時的控制寬度

XAML代碼如下:

<UserControl x:Class="MyReader.FeedControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}"    
Loaded="UserControl_Loaded" > 

<Grid x:Name="LayoutRoot" Height="50" Background="Black" HorizontalAlignment="Left" > 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="70"/> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="70"/> 
    </Grid.ColumnDefinitions> 
    <CheckBox Name="Select" Grid.Column="0" IsChecked="{Binding IsSelected}" Margin="-5,-16" Checked="Select_Checked" Background="White"/> 
    <TextBlock Name="FeedName" Grid.Column="1" Text="{Binding FeedName}" Opacity="1" Margin="-20" VerticalAlignment="Center" FontSize="24" 
       MouseLeftButtonUp="FeedName_MouseLeftButtonUp" Height="32" Width="360"/> 
    <TextBlock Name="UnreadItems" Grid.Column="2" Text="{Binding UnreadItems}" Opacity="1" Padding="2" VerticalAlignment="Center" HorizontalAlignment="Right" 
       FontSize="24" MouseLeftButtonUp="FeedName_MouseLeftButtonUp" /> 
</Grid> 

當頁面的方向變化的Feedcontrol項目列表框是我想進名文本塊更改爲正確的尺寸(560像素景觀,縱向爲360px),所以我假定通過將網格欄寬度設置爲「*」或「自動」,並設置文本塊沒有寬度,它會自動調整大小,但只調整爲文本寬度而不是全寬。

於是我嘗試了文本塊設置在XAML和頁面方向改變事件我叫下面的方法在FeedControl的每個實例目前ListBox中默認的縱向尺寸(360):

public void ChangeOrientation(bool isLandscape) 
    { 

     if (isLandscape) 
     { 
      this.LayoutRoot.Width = App.LandscapeWidth;  //700 
      this.FeedName.Width = App.LandscapeItemWidth; //560 
     } 
     else 
     { 
      this.LayoutRoot.Width = App.PortraitWidth;  //480 
      this.FeedName.Width = App.PortraitItemWidth; //360 
     } 
     this.InvalidateArrange(); 
     this.InvalidateMeasure(); 
    } 

然而,這也不管用(不管我嘗試什麼),所以我很困惑如何最好地做到這一點......我知道必須有一個非常簡單的方法,但到目前爲止我還沒有找到它。

請問有人可以指點我正確的方向嗎?

邁克

PS對不起後的長度。所以,只要這樣一個簡單的問題!

+0

低質量,注意明確 – 2013-07-30 14:24:33

回答

0

你在哪裏放置這個控件,你沒有提到。我想你是把這個控件放在電話頁面裏面;像放置任何其他控件。如果這樣更好,您可以在該頁面(容器頁面)中管理與定位有關的東西,而不是在控件中進行調整。我認爲這會解決你的問題。

+0

列表框(包含我的控制中多次出現)做,這是一個正常的手機頁面你懷疑,但我不明白你的回答會如何幫助對不起......特別是,「更好地管理與定向有關的東西」是什麼意思?控件中textblock的寬度需要按照不同的方式進行更改,我正在試圖找出如何做到最好(或最正確)的方式......您能更具體嗎? – nzmike 2011-05-25 22:39:02

2

您應該將HorizontalAlignmentHorizontalContentAlignment設置爲HorizontalAlignment。對於有您的控制權的ListBoxItems,可以使用Stretch。另外,設置控件的設計寬度/設計高度。

下面是我在列表框中的一個控件的示例,它在橫向模式下延伸。

<UserControl x:Class="SabWatch.Resources.Controls.ProgressBox" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="480" d:DesignWidth="480"> 

    <Grid x:Name="LayoutRoot" Style="{StaticResource AppBackgroundStyle}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 

    </Grid> 
</UserControl> 

下面是在代碼中設置對齊屬性的示例。您還可以在XAML(以及可能使用的風格,並給予ListBox一個ItemContainerStyle

var lbi = new ListBoxItem(); 
      var box = new ProgressBox(); 
... 
      lbi.Tag = queueObject; 
      lbi.Content = box; 
      lbi.HorizontalAlignment = HorizontalAlignment.Stretch; 
      lbi.HorizontalContentAlignment = HorizontalAlignment.Stretch; 
+0

順便提一下,我問這個問題的答案;) – 2011-05-26 06:39:20

+0

http:// stackoverflow。com/questions/5373818/usercontrol-grid-gets-displayed-with-portrait-sizing-in-landscape-mode – 2011-05-26 06:39:35

+0

感謝您的支持 - 非常感謝。我最終發現,在我的網格定義中使用Auto並手動更改每個用戶控件實例中TextBlock的寬度,但您的方式看起來更精緻,所以我會嘗試。 :-) – nzmike 2011-05-28 04:05:05

相關問題