Top Dock in a DockPanel is taking all the space.

huangapple go评论55阅读模式
英文:

Top Dock in a DockPanel is taking all the space

问题

我的代码看起来类似于以下内容:

<DockPanel>
  <Expander DockPanel.Dock="Top" Name="Expander1">
    <local:ListView1 DataContext="{Binding Source1}"/>
  </Expander>
  <Expander DockPanel.Dock="Top" Name="Expander2">
    <local:ListView1 DataContext="{Binding Source2}"/>
  </Expander>
  <Expander DockPanel.Dock="Top" Name="Expander3">
    <local:ListView1 DataContext="{Binding Source3}"/>
  </Expander>
</DockPanel>

ListView1只是一个包含ListView的用户控件。

我已经设置了以下行为:当一个Expander打开时,其他两个Expander将关闭。

问题是,当Expander1打开并且其内容超过窗口高度时,它将具有滚动条以滚动其内容,而Expander2Expander3不会显示。我认为Expander1占据了UI上的所有空间,将Expander2Expander3推出了UI。当Expander2打开时,Expander3被推出了UI并且不会显示。我应该怎么做,以便打开一个Expander时,其下面的Expander不会被推出UI?

英文:

My code looks something like this:

&lt;DockPanel&gt;
  &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander1&quot;&gt;
     &lt;local:ListView1 DataContext=&quot;{Binding Source1}&quot;/&gt;
  &lt;/Expander&gt;
  &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander2&quot;&gt;
     &lt;local:ListView1 DataContext=&quot;{Binding Source2}&quot;/&gt;
  &lt;/Expander&gt;
  &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander3&quot;&gt;
     &lt;local:ListView1 DataContext=&quot;{Binding Source3}&quot;/&gt;
  &lt;/Expander&gt;
&lt;/DockPanel&gt;

ListView1 is just a user control that contains a ListView

I have the behavior set: when one Expander is open, all the other 2 Expanders will close.

Top Dock in a DockPanel is taking all the space.

Top Dock in a DockPanel is taking all the space.

The problem is that when Expander1 is open and its content is more than the window height, it will have a scroll bar to scroll down for its content while Expander2 and Expander3 are not displayed. I think Expander1 uses all the space on the UI and Expander2 & Expander3 get pushed out side of the UI. When Expander2 is open, Expander3 is pushed out of the UI and not displayed. What can I do so that when I open an Expander, the one(s) below it won't get pushed out of the UI?

答案1

得分: 1

你说你一次只允许打开一个。

鉴于此,尺寸逻辑更简单,你可以只使用一个带有固定数字的转换器。

我的演示:

<DockPanel>
    <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}">
        <ListBox ItemsSource="{Binding Items}"/>
    </Expander>
    <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}">
        <ListBox ItemsSource="{Binding Items}"/>
    </Expander>
    <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}">
        <ListBox ItemsSource="{Binding Items}"/>
    </Expander>
</DockPanel>

以及转换器:

public class AddConverter : MarkupExtension, IValueConverter
{
    public double ValueToAdd { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double bound = (Double)value;
        return bound + ValueToAdd;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

正如我在评论中提到的,

你可以使用多绑定和多转换器来更加复杂化这个过程。这可以允许在打开两个时设置高度。

英文:

You said you're only allowing one open at a time.

Given that then the size logic is simpler and you could use just a converter with a fixed number.

My PoC:

&lt;DockPanel&gt;
    &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
        &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
    &lt;/Expander&gt;
    &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
        &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
    &lt;/Expander&gt;
    &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight, 
        RelativeSource={RelativeSource AncestorType=ContentPresenter}, 
        Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
        &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
    &lt;/Expander&gt;
&lt;/DockPanel&gt;

And the converter

public class AddConverter : MarkupExtension, IValueConverter
{
    public double ValueToAdd { get; set; }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double bound = (Double)value;
        return bound + ValueToAdd;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

Top Dock in a DockPanel is taking all the space.

As I mentioned in the comments.

You could make this more sophisticated with a multibinding and multiconverter. That could allow for height when 2 are open.

huangapple
  • 本文由 发表于 2023年2月14日 03:52:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440586.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定