WPF UI AddIn未更新其所需大小

时间:2010-12-30 20:15:20

标签: c# wpf add-in

我有一个应用程序通过来自不同AppDomain的INativeHandleContract获取其UI控件之一。当控件的大小更改时,主机中的FrameworkElement不会获得更新的大小。我想知道是否有办法解决这个问题。

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

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

当您展开并折叠Expander时,您会期望StackPanel的高度发生变化,但事实并非如此。任何想法都会有用,谢谢。

1 个答案:

答案 0 :(得分:0)

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

  1. 将Expander包装在“虚拟”StackPanel中,并将其用作m_expanderAddIn的根元素。这照顾了Expander的高度问题。
  2. 将m_expanderAddIn的类型从UIElement更改为FrameworkElement
  3. 将m_expanderAddIn的高度绑定到m_expander的ActualHeight
  4. <强>代码

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