Top Dock in a DockPanel is taking all the space.

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

Top Dock in a DockPanel is taking all the space

问题

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

  1. <DockPanel>
  2. <Expander DockPanel.Dock="Top" Name="Expander1">
  3. <local:ListView1 DataContext="{Binding Source1}"/>
  4. </Expander>
  5. <Expander DockPanel.Dock="Top" Name="Expander2">
  6. <local:ListView1 DataContext="{Binding Source2}"/>
  7. </Expander>
  8. <Expander DockPanel.Dock="Top" Name="Expander3">
  9. <local:ListView1 DataContext="{Binding Source3}"/>
  10. </Expander>
  11. </DockPanel>

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

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

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

英文:

My code looks something like this:

  1. &lt;DockPanel&gt;
  2. &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander1&quot;&gt;
  3. &lt;local:ListView1 DataContext=&quot;{Binding Source1}&quot;/&gt;
  4. &lt;/Expander&gt;
  5. &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander2&quot;&gt;
  6. &lt;local:ListView1 DataContext=&quot;{Binding Source2}&quot;/&gt;
  7. &lt;/Expander&gt;
  8. &lt;Expander DockPanel.Dock=&quot;Top&quot; Name=&quot;Expander3&quot;&gt;
  9. &lt;local:ListView1 DataContext=&quot;{Binding Source3}&quot;/&gt;
  10. &lt;/Expander&gt;
  11. &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

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

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

我的演示:

  1. <DockPanel>
  2. <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight,
  3. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  4. Converter={local:AddConverter ValueToAdd=-46}}">
  5. <ListBox ItemsSource="{Binding Items}"/>
  6. </Expander>
  7. <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight,
  8. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  9. Converter={local:AddConverter ValueToAdd=-46}}">
  10. <ListBox ItemsSource="{Binding Items}"/>
  11. </Expander>
  12. <Expander DockPanel.Dock="Top" MaxHeight="{Binding ActualHeight,
  13. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  14. Converter={local:AddConverter ValueToAdd=-46}}">
  15. <ListBox ItemsSource="{Binding Items}"/>
  16. </Expander>
  17. </DockPanel>

以及转换器:

  1. public class AddConverter : MarkupExtension, IValueConverter
  2. {
  3. public double ValueToAdd { get; set; }
  4. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  5. {
  6. double bound = (Double)value;
  7. return bound + ValueToAdd;
  8. }
  9. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  10. {
  11. return null;
  12. }
  13. public override object ProvideValue(IServiceProvider serviceProvider)
  14. {
  15. return this;
  16. }
  17. }

正如我在评论中提到的,

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

英文:

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:

  1. &lt;DockPanel&gt;
  2. &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight,
  3. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  4. Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
  5. &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
  6. &lt;/Expander&gt;
  7. &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight,
  8. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  9. Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
  10. &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
  11. &lt;/Expander&gt;
  12. &lt;Expander DockPanel.Dock=&quot;Top&quot; MaxHeight=&quot;{Binding ActualHeight,
  13. RelativeSource={RelativeSource AncestorType=ContentPresenter},
  14. Converter={local:AddConverter ValueToAdd=-46}}&quot;&gt;
  15. &lt;ListBox ItemsSource=&quot;{Binding Items}&quot;/&gt;
  16. &lt;/Expander&gt;
  17. &lt;/DockPanel&gt;

And the converter

  1. public class AddConverter : MarkupExtension, IValueConverter
  2. {
  3. public double ValueToAdd { get; set; }
  4. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  5. {
  6. double bound = (Double)value;
  7. return bound + ValueToAdd;
  8. }
  9. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  10. {
  11. return null;
  12. }
  13. public override object ProvideValue(IServiceProvider serviceProvider)
  14. {
  15. return this;
  16. }
  17. }

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:

确定