1

私のアプリケーションでは、FrameworkElementカスタム関数を使用してアタッチされたビューモデルオブジェクトを取得していますGetVisualChildWithDataContext(以下を参照)。

何が起こっているのかというと、この関数は、プロパティが0のオブジェクトを返します。ContentPresenterコンテンツは24x24の長方形で構成されているため、2つのプロパティの値は24になると予想されます。ActualWidthActualHeight

私の質問は、ビューモデルだけを使用して、期待している値(ActualWidthおよびActualHeight24の値)を取得するにはどうすればよいですか?これを行うためのより良い方法があれば、私はすべての耳です。

御時間ありがとうございます。(以下のサンプルを参照)

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

2 に答える 2

0

このリンクで説明されている手法を使用して、にバインドしましActualWidthActualHeight

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

著者は彼の技術を「プッシュバインディング」と呼んでいます。

于 2012-06-13T21:28:11.547 に答える
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);
}

きれいではありませんが、それは仕事を成し遂げます。

于 2012-06-14T14:05:03.707 に答える