簡體   English   中英

無法檢索ContentPresenter的ActualWidth / ActualHeight

[英]Unable to retrieve the ActualWidth / ActualHeight of ContentPresenter

在我的應用程序中,我們有一個視圖模型對象,可使用自定義函數GetVisualChildWithDataContext檢索其附加的FrameworkElement (請參見下文)。

發生的情況是此函數返回一個ContentPresenter對象,其ActualWidthActualHeight屬性為0。由於我的內容由24x24矩形組成,因此我希望這2個屬性的值都為24。

我的問題是,如何僅具有視圖模型來檢索期望的值( 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; }
}

我已使用此鏈接中描述的技術綁定到ActualWidthActualHeight

http://meleak.wordpress.com/2011/08/28/onewaytosource-binding-for-readonly-dependency-property/

作者稱其技術為“推式裝訂”。

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

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);
}

不漂亮,但是可以完成工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM