我一直在与ListBox争论这个问题很长一段时间。我有一个用于显示股票期权链的MVVM应用程序。
OptionChainViewModel创建每个链 - 每个可用的到期时间链。
然后我尝试用ListBox显示这些“链”,ListBox有一个ItemsPanelTemplate,我有一个StackPanel。
虽然我可以显示HorizontalScrollBar,但它不活动,我无法让内容水平滚动。我已经尝试在StackPanel上设置MaxWidth以及ListBox本身无济于事。我还在OptionViewModel对象上设置MaxWidth和Width无济于事。
我查看了可视化树,在ListBoxItems之前找到了scrollviewer,并将MaxWidth和MaxHeight设置为“infinity”,因此我在viewmodel上插入了一个方法,当我单击视图中的按钮时调用该方法。该方法找到scrollviewer并为scrollviewer设置MaxWidth和MaxHeight值......但即使这样也不会导致水平滚动条工作。
<ListBox Name="lbAllOptions"
Width="600"
MaxHeight="400"
MaxWidth="500"
IsSynchronizedWithCurrentItem="True"
ScrollViewer.CanContentScroll="True"
ScrollViewer.HorizontalScrollBarVisibility="Visible"
ScrollViewer.VerticalScrollBarVisibility="Hidden"
ItemsSource="{Binding AllChains}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
<!-- <pnls:CustomPanel MaxWidth="500" MaxHeight="400"/>-->
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
每个选项链项的相关代码是:
<DockPanel>
<GroupBox MaxWidth="500"
HorizontalAlignment="Left">
<GroupBox.HeaderTemplate>
<DataTemplate>
<StackPanel x:Name="stack"
Orientation="Horizontal">
<TextBlock Text="{Binding Source={x:Reference tblockDName}, Path=Text}"
DataContext="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type GroupBox}}}" />
</StackPanel>
</DataTemplate>
</GroupBox.HeaderTemplate>
<Grid DockPanel.Dock="Bottom"
Margin="0,2,4,2">
<StackPanel Grid.Row="0"
Grid.Column="1"
MaxHeight="300"
MaxWidth="400"
Orientation="Vertical">
<TextBlock x:Name="tblockDName"
Text="{Binding Path=DisplayName}"
Visibility="Collapsed" />
<Label Content="Calls"
HorizontalAlignment="Left"
Name="lblCallOptionChains"
VerticalAlignment="Top" />
<DataGrid MinHeight="200"
MinWidth="200"
MaxWidth="500"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
EnableRowVirtualization="True"
AlternatingRowBackground="LightGray"
SelectionUnit="FullRow"
Name="dgridCallOptionChain"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding _CallOptionList}"
SelectedItem="{Binding Path=SelectedOption, Mode=TwoWay}"
VerticalAlignment="Stretch"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
MaxHeight="200"
IsManipulationEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="SampleKeyDownEventHandler" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewTextInput">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="SampleTextInputEventHandler" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Symbol"
Binding="{Binding OCSymbol}" />
<DataGridTextColumn Header="Strike"
Binding="{Binding OCStrike}" />
<DataGridTextColumn Header="Mid"
Binding="{Binding OCMid}" />
<DataGridTextColumn Header="Interest"
Binding="{Binding OCInterest}" />
<DataGridTextColumn Header="Model"
Binding="{Binding OCModel}" />
<DataGridTextColumn Header="Position"
Binding="{Binding OCPosition}" />
</DataGrid.Columns>
</DataGrid>
<Label Content="Puts"
HorizontalAlignment="Left"
Name="lblPutOptionChains"
VerticalAlignment="Top" />
<DataGrid MinHeight="200"
MinWidth="200"
MaxWidth="500"
HorizontalAlignment="Left"
AutoGenerateColumns="False"
EnableRowVirtualization="True"
AlternatingRowBackground="LightGray"
SelectionUnit="FullRow"
Name="dgridPutOptionChain"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding _PutOptionList}"
SelectedItem="{Binding Path=SelectedOption, Mode=TwoWay}"
VerticalAlignment="Stretch"
HorizontalScrollBarVisibility="Visible"
VerticalScrollBarVisibility="Visible"
MaxHeight="200"
IsManipulationEnabled="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewKeyDown">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="SampleKeyDownEventHandler" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewTextInput">
<ei:CallMethodAction TargetObject="{Binding}"
MethodName="SampleTextInputEventHandler" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Symbol"
Binding="{Binding OCSymbol}" />
<DataGridTextColumn Header="Strike"
Binding="{Binding OCStrike}" />
<DataGridTextColumn Header="Mid"
Binding="{Binding OCMid}" />
<DataGridTextColumn Header="Interest"
Binding="{Binding OCInterest}" />
<DataGridTextColumn Header="Model"
Binding="{Binding OCModel}" />
<DataGridTextColumn Header="Position"
Binding="{Binding OCPosition}" />
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Grid>
</GroupBox>
</DockPanel>
我尝试了各种方法,例如尝试将ListBox包装在ScrollViewer中,设置“CanContentScroll”的各种组合似乎没有任何结果导致水平滚动条可用。
我甚至创建了一个实现IScrollInfo的CustomPanel,希望我可以让它滚动内容......这也不起作用。
所以......我回头拉毛,没有多少人留下! 提前感谢您提供的任何帮助。
答案 0 :(得分:0)
除非我误解了你,否则我之前接触过的方法是将包含控件的所有代码设置为列表框的ItemTemplate(DockPanel位也不需要) 。然后将所有必需的位绑定到链类的属性,而不是让您的itemssource充满DockPanels