WPF UI AddIn 未更新其所需的大小

发布于 2024-10-09 23:03:57 字数 1571 浏览 0 评论 0原文

我有一个应用程序,它通过 INativeHandleContract 从不同的 AppDomain 获取其 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 的高度发生变化,但事实并非如此。任何想法都会有用,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影 2024-10-16 23:03:57

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

  1. 将 Expander 包装在“虚拟”StackPanel 中,并将其用作 m_expanderAddIn 的根元素。这解决了扩展器的高度问题。
  2. 将 m_expanderAddIn 的类型从 UIElement 更改为 FrameworkElement
  3. 将 m_expanderAddIn 的高度绑定到 m_expander

Code的 ActualHeight

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文