英文:
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
打开并且其内容超过窗口高度时,它将具有滚动条以滚动其内容,而Expander2
和Expander3
不会显示。我认为Expander1
占据了UI上的所有空间,将Expander2
和Expander3
推出了UI。当Expander2
打开时,Expander3
被推出了UI并且不会显示。我应该怎么做,以便打开一个Expander时,其下面的Expander不会被推出UI?
英文:
My code looks something like this:
<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
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.
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:
<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>
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;
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论