2012-06-13 64 views
1

在我的應用程序中,我們有一個視圖模型對象,我們使用我們的自定義函數GetVisualChildWithDataContext(參見下文)從中檢索其附加的FrameworkElement無法檢索ContentPresenter的ActualWidth/ActualHeight

發生了什麼事是,這個函數返回一個ContentPresenter對象及其ActualWidthActualHeight性質爲0。我會期望得到的值24的2個屬性,因爲我的內容包括一個24x24的矩形。

我的問題是,如何檢索我期望的值(ActualWidthActualHeight爲24)只有一個視圖模型?如果有更好的方法來做到這一點,我就會全神貫注。

謝謝你的時間。 (見下面的樣品)

XAML:

<Window x:Class="WpfApplication2.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:l="clr-namespace:WpfApplication2" 
     Title="MainWindow" 
     Width="525" 
     Height="350"> 
    <Canvas x:Name="_RootCanvas"> 
    <ItemsControl Margin="-5,0,0,0" ItemsSource="{Binding MyItems}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
     <Canvas /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate DataType="{x:Type l:MyItem}"> 
     <Canvas> 
      <Rectangle 
       Canvas.Left="{Binding ActualLeft}" 
       IsHitTestVisible="True" 
       Margin="0,0,0,0" 
       Width="24" 
       Height="24" 
       Fill="Black" /> 
     </Canvas> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
    </ItemsControl> 

    <Button Canvas.Top="50" Width="100" Height="30" Click="Button_Clicked"> 
     <TextBlock Text="Button" /> 
    </Button> 
    </Canvas> 
</Window> 

代碼後面:

private MyItem _MyItem; 
public MainWindow() 
{ 
    InitializeComponent(); 

    _MyItem = new MyItem(0); 
    MyItems = new ObservableCollection<MyItem>(); 
    MyItems.Add(_MyItem); 

    DataContext = this; 
} 

public ObservableCollection<MyItem> MyItems { get; set; } 

public void Button_Clicked(object sender, RoutedEventArgs e) 
{ 
    FrameworkElement fe = GetVisualChildWithDataContext(_RootCanvas, _MyItem); 
} 

public static FrameworkElement GetVisualChildWithDataContext(DependencyObject parent, object dataContext) 
{ 
    int count = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < count; i++) 
    { 
     DependencyObject d = VisualTreeHelper.GetChild(parent, i); 
     FrameworkElement element = d as FrameworkElement; 
     if (element != null) 
     { 
      if (element.DataContext == dataContext) 
       return element; 

      var f = GetVisualChildWithDataContext(element, dataContext); 
      if (f != null) 
       return f; 
     } 
    } 

    return null; 
} 

public class MyItem 
{ 
    public MyItem(double left) 
    { 
     ActualLeft = left; 
    } 
    public double ActualLeft { get; set; } 
} 

回答

0

要解決我的問題,我創建了下面的擴展方法:

public static Size GetActualSize(this FrameworkElement parent) 
{ 
    var contentPresenter = parent as ContentPresenter; 
    if (contentPresenter != null) 
    { 
     Rect descendantBounds = VisualTreeHelper.GetDescendantBounds(contentPresenter); 
     if (!descendantBounds.Size.IsEmpty) 
      return descendantBounds.Size; 
    } 

    return new Size(parent.ActualWidth, parent.ActualHeight); 
} 

不漂亮,但它能夠完成任務。