WPF UI AddIn not updating its desired size

纵饮孤独 提交于 2019-12-13 02:39:04

问题


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.

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

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.


回答1:


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. Wrap the Expander in a "dummy" StackPanel and use that as the root element for m_expanderAddIn instead. This took care of the Height problem for the Expander.
  2. Change the type of m_expanderAddIn from UIElement to FrameworkElement
  3. Bind the Height of m_expanderAddIn to ActualHeight of m_expander

Code

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


来源:https://stackoverflow.com/questions/4565926/wpf-ui-addin-not-updating-its-desired-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!