2009-11-01 24 views
2

我仍然在學習WPF,但我真的很困惑,應該很簡單。我想要做的是集中第3和第4列的內容。當我運行此,列左對齊:WPF - 如何在列表視圖中居中列數據?

<ListView Margin="0" x:Name="listMonitoredUrls" AlternationCount="1" 
      ItemsSource="{Binding}" > 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Description" DisplayMemberBinding="{Binding FriendlyDesc}"/> 
      <GridViewColumn Header="Url" DisplayMemberBinding="{Binding Url}"/> 
      <GridViewColumn Header="Frequency"> 
       <GridViewColumn.CellTemplate > 
        <DataTemplate> 
         <TextBlock Text="{Binding ScanFrequencyMinutes}" 
            HorizontalAlignment="Center"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
      <GridViewColumn Header="Next Scan" > 
       <GridViewColumn.CellTemplate > 
        <DataTemplate> 
         <TextBlock Text="{Binding TimeNextScanStr}" 
            HorizontalAlignment="Center"/> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 
     </GridView> 
    </ListView.View> 
</ListView> 

我「米真的開始喜歡WPF,但類似這樣的一些簡單的事情似乎真的很難

回答

0

嘗試使用TextAlignment屬性,而不是HorizontalAlignment - 應該這樣做 據我瞭解HorizontalAlignment="Center"將圍繞你的文本塊不是它的文本

+0

這是我在我的第一次嘗試使用。它也不起作用。對不起,沒有把我的最初發布。 – 2009-11-01 17:47:37

+0

它是否工作,如果你左或右?檢查這個鏈接,看看你是否可以發現與你的xaml的區別 - > http://social.msdn.microsoft.com/forums/en-US/wpf/thread/1f17a5a6-c71f-4810-b8bc-e4eb2a25fa96/ – JohnIdol 2009-11-01 18:10:09

0

這可能是一個長鏡頭,但我不得不這樣做。對於其中的項目是由模板定義列表框嘗試。在ListView上設置Horizo​​ntalContentAlignment =「Stretch」我並沒有設定這些物品只佔用足夠的空間,並且是有理由的。

0

我已經創建了其中常見的情況下有效的解決方案:

<GridViewColumn Header="Some Property" DisplayMemberBinding="{Binding SomeProperty}" /> 

,其中一個只想要一個簡單的DisplayMemberBinding文字,而無需指定CellTemplate

新的代碼使用附加屬性和變成:

<GridViewColumn Header="Some Property" DisplayMemberBinding="{Binding SomeProperty}" 
       ctrl:GridViewExtensions.IsContentCentered="True" /> 

附加屬性代碼:

public static class GridViewExtensions 
{ 
    #region IsContentCentered 

    [Category("Common")] 
    [AttachedPropertyBrowsableForType(typeof(GridViewColumn))] 
    public static bool GetIsContentCentered(GridViewColumn gridViewColumn) 
    { 
     return (bool)gridViewColumn.GetValue(IsContentCenteredProperty); 
    } 
    public static void SetIsContentCentered(GridViewColumn gridViewColumn, bool value) 
    { 
     gridViewColumn.SetValue(IsContentCenteredProperty, value); 
    } 

    public static readonly DependencyProperty IsContentCenteredProperty = 
     DependencyProperty.RegisterAttached(
      "IsContentCentered", 
      typeof(bool), // type 
      typeof(GridViewExtensions), // containing type 
      new PropertyMetadata(default(bool), OnIsContentCenteredChanged) 
      ); 

    private static void OnIsContentCenteredChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     OnIsContentCenteredChanged((GridViewColumn)d, (bool)e.NewValue); 
    } 
    private static void OnIsContentCenteredChanged(GridViewColumn gridViewColumn, bool isContentCentered) 
    { 
     if (isContentCentered == false) { return; } 
     // must wait a bit otherwise GridViewColumn.DisplayMemberBinding will not yet be initialized, 
     new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Normal, OnColumnLoaded, gridViewColumn.Dispatcher) 
     { 
      Tag = gridViewColumn 
     }.Start(); 
    } 

    static void OnColumnLoaded(object sender, EventArgs e) 
    { 
     var timer = (DispatcherTimer)sender; 
     timer.Stop(); 

     var gridViewColumn = (GridViewColumn)timer.Tag; 
     if (gridViewColumn.DisplayMemberBinding == null) 
     { 
      throw new Exception("Only allowed with DisplayMemberBinding."); 
     } 
     var textBlockFactory = new FrameworkElementFactory(typeof(TextBlock)); 
     textBlockFactory.SetBinding(TextBlock.TextProperty, gridViewColumn.DisplayMemberBinding); 
     textBlockFactory.SetValue(TextBlock.TextAlignmentProperty, TextAlignment.Center); 
     var cellTemplate = new DataTemplate { VisualTree = textBlockFactory }; 
     gridViewColumn.DisplayMemberBinding = null; // must null, otherwise CellTemplate won't be recognized 
     gridViewColumn.CellTemplate = cellTemplate; 
    } 

    #endregion IsContentCentered 

}

+0

note我沒有徹底測試'DispatcherTimer'間隔,所以你可能想搞砸它。 – 2012-02-06 21:42:00

+0

另外,請注意,我的解決方案沒有考慮更改值。 (即,如果「IsContentCentered」值從true改變爲false,則綁定不會恢復到左對齊) – 2012-02-06 21:52:28