两个不同库中 ScrollViewer 内的 Wrappanel
Wrappanel inside ScrollViewer in two different libraries
我是 wpf 和 mvvm 的新手,所以我不想寻求帮助。抱歉,英语不是我的母语。
我有显示一些文档的主要应用程序。每个文档都是一个外部 dll + xml-带有数据的文件。
在主应用程序中我有 XAML:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</ScrollViewer>
在 ContentControl 中,我将我的文档视图显示为来自 dll 的 UserControl。我们使用 MVVM,所以文档的 XAML 是这样的:
<UserControl>
<StackPanel>
<UserControl_Type1 />
<UserControl_Type2 />...
</StackPanel>
<UserControl_Type3 />
</UserControl>
这些 UserControl_TypeXXX 中的任何一个都可以在其自己的 dll 中。
现在用户要求我将 StackPanel 替换为 WrapPanel 以获得某种类型的 UserControl。而且,当用户调整主应用程序大小时,一些控件首先包装它们的元素,然后才显示所有文档的滚动条。
但是根据我读过的信息,WrapPanel 并没有在 ScrollViewer 中换行。
如果我没有来自主应用程序的宽度和高度并且无法预测我的文档中的哪个 UserControl 应该包装他们的元素,我该如何实现此行为?
我找到了这个 article about xaml anti-patterns,它对我帮助很大。
在我的例子中,我在两个内容模板之间切换。一个带有用于旧文档的 ScrollViewer,另一个 - 没有。
在资源中:
<DataTemplate x:Key="ResizableContent" >
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</Border>
</DataTemplate>
<DataTemplate x:Key="NotResizableContent" >
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</ScrollViewer>
</DataTemplate>
在主面板中:
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource NotResizableContent}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsResizable}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource ResizableContent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
我是 wpf 和 mvvm 的新手,所以我不想寻求帮助。抱歉,英语不是我的母语。 我有显示一些文档的主要应用程序。每个文档都是一个外部 dll + xml-带有数据的文件。
在主应用程序中我有 XAML:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</ScrollViewer>
在 ContentControl 中,我将我的文档视图显示为来自 dll 的 UserControl。我们使用 MVVM,所以文档的 XAML 是这样的:
<UserControl>
<StackPanel>
<UserControl_Type1 />
<UserControl_Type2 />...
</StackPanel>
<UserControl_Type3 />
</UserControl>
这些 UserControl_TypeXXX 中的任何一个都可以在其自己的 dll 中。
现在用户要求我将 StackPanel 替换为 WrapPanel 以获得某种类型的 UserControl。而且,当用户调整主应用程序大小时,一些控件首先包装它们的元素,然后才显示所有文档的滚动条。 但是根据我读过的信息,WrapPanel 并没有在 ScrollViewer 中换行。
如果我没有来自主应用程序的宽度和高度并且无法预测我的文档中的哪个 UserControl 应该包装他们的元素,我该如何实现此行为?
我找到了这个 article about xaml anti-patterns,它对我帮助很大。
在我的例子中,我在两个内容模板之间切换。一个带有用于旧文档的 ScrollViewer,另一个 - 没有。
在资源中:
<DataTemplate x:Key="ResizableContent" >
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</Border>
</DataTemplate>
<DataTemplate x:Key="NotResizableContent" >
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="DarkGray">
<Border Margin="20" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="White" Padding="20">
<ContentControl x:Name="ccCurrentView" cal:View.Model="{Binding Path=CurrentView}"/>
</Border>
</ScrollViewer>
</DataTemplate>
在主面板中:
<ContentControl Content="{Binding}">
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Setter Property="ContentTemplate" Value="{StaticResource NotResizableContent}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Path=IsResizable}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource ResizableContent}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>