WPF UI加载项未更新其所需大小 [英] WPF UI AddIn not updating its desired size

查看:138
本文介绍了WPF UI加载项未更新其所需大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它通过INativeHandleContract从其他AppDomain获取其UI控件之一。当控件的大小更改时,主机中的FrameworkElement不会得到更新的大小。我想知道是否有任何方法可以解决此问题。

I have an application that gets one of its UI controls via an INativeHandleContract from a different AppDomain. When the size of the control changes the FrameworkElement in the host doesn't get an updated size. I was wondering if there was any way to fix this.

这里是示例代码(XAML只是一个空白窗口):

Here is the sample code (the XAML is just a blank window):

public partial class Window1 : Window
{
    private StackPanel m_stackPanel;
    private Expander m_expander;

    private UIElement m_expanderAddIn;

    public Window1()
    {
        InitializeComponent();

        m_stackPanel = new StackPanel { Orientation = Orientation.Vertical };
        m_stackPanel.Background = Brushes.Red;
        m_expander = new Expander
                         {
                             ExpandDirection = ExpandDirection.Right,
                             Background=Brushes.Blue,
                             IsExpanded=true,
                         };
        m_expander.Expanded += CheckStuff;
        m_expander.Collapsed += CheckStuff;

        Rectangle r = new Rectangle {Fill = Brushes.LightGray, Height = 300, Width = 300};

        m_expander.Content = r;

        m_expanderAddIn = FrameworkElementAdapters.ContractToViewAdapter(FrameworkElementAdapters.ViewToContractAdapter(m_expander));

        m_stackPanel.Children.Add(m_expanderAddIn);

        Content = m_stackPanel;
    }

    private void CheckStuff(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Expander: " + m_expander.DesiredSize);
        Debug.WriteLine("Add in: " + m_expanderAddIn.DesiredSize);
        Debug.WriteLine("Stack Panel: " + m_stackPanel.DesiredSize);
    }
}

在展开和折叠扩展器时,您会期望StackPanel的高度可以更改,但不会更改。谢谢任何想法。

As you expand and collapse the Expander you would expect the height of the StackPanel to change but it doesn't. Any ideas would be useful, thanks.

推荐答案

那真的很奇怪,我也不知道为什么会这样。这可能只是重现您的问题的一个小示例,所以也许对您没有太大帮助,但是我让您的示例进行了3次更改。

That's really weird and I have no idea why it behaves like that. This is probably just a small example reproducing your problem so maybe this doesn't help you much but I got your example working with 3 changes.


  1. 将扩展器包装在虚拟 StackPanel中,然后将其用作m_expanderAddIn的根元素。

  2. 将m_expanderAddIn的类型从UIElement更改为FrameworkElement

  3. 将m_expanderAddIn的高度绑定为ActualHeight m_expander

代码

public partial class Window1 : Window
{
    private StackPanel m_stackPanel;
    private Expander m_expander;

    private FrameworkElement m_expanderAddIn;

    public Window1()
    {
        InitializeComponent();

        m_stackPanel = new StackPanel { Orientation = Orientation.Vertical };
        m_stackPanel.Background = Brushes.Red;
        m_expander = new Expander
                         {
                             ExpandDirection = ExpandDirection.Right,
                             Background=Brushes.Blue,
                             IsExpanded=true,
                         };
        m_expander.Expanded += CheckStuff;
        m_expander.Collapsed += CheckStuff;

        Rectangle r = new Rectangle {Fill = Brushes.LightGray, Height = 300, Width = 300};

        m_expander.Content = r;
        StackPanel stackPanel = new StackPanel();
        stackPanel.Children.Add(m_expander);

        m_expanderAddIn = FrameworkElementAdapters.ContractToViewAdapter(FrameworkElementAdapters.ViewToContractAdapter(stackPanel));

        Binding binding = new Binding("ActualHeight");
        binding.Source = m_expander;
        m_expanderAddIn.SetBinding(FrameworkElement.HeightProperty, binding);

        m_stackPanel.Children.Add(m_expanderAddIn);

        Content = m_stackPanel;
    }

    private void CheckStuff(object sender, RoutedEventArgs e)
    {
        Debug.WriteLine("Expander: " + m_expander.DesiredSize);
        Debug.WriteLine("Add in: " + m_expanderAddIn.DesiredSize);
        Debug.WriteLine("Stack Panel: " + m_stackPanel.DesiredSize);
    }
}

这篇关于WPF UI加载项未更新其所需大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆