首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法检索ContentPresenter的ActualWidth / ActualHeight

无法检索ContentPresenter的ActualWidth / ActualHeight
EN

Stack Overflow用户
提问于 2012-06-14 05:19:49
回答 2查看 1.9K关注 0票数 1

在我的应用程序中,我们有一个视图模型对象,我们使用自定义函数GetVisualChildWithDataContext (如下所示)从该对象检索附加的FrameworkElement

这个函数返回一个ActualWidthActualHeight属性为0的ContentPresenter对象。我希望这两个属性的值为24,因为我的内容由一个24x24的矩形组成。

我的问题是,如何通过只有一个视图模型来检索我期望的值(ActualWidthActualHeight为24)?如果有更好的办法,我洗耳恭听。

耽误您时间,实在对不起。(参见下面的示例)

xaml:

代码语言:javascript
复制
<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>

代码隐藏:

代码语言:javascript
复制
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; }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-06-14 22:05:04

为了解决我的问题,我创建了以下扩展方法:

代码语言:javascript
复制
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);
}

不是很漂亮,但它能完成工作。

票数 0
EN

Stack Overflow用户

发布于 2012-06-14 05:28:12

我已经使用此链接中描述的技术绑定到ActualWidthActualHeight

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

作者将他的技术称为“推绑定”。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11023468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档