问题描述
您好,目前,我正在尝试构建一个具有ContentControl的应用程序,该控件可在派生的DataGrid和ListBox之间切换,以提供有关Data的两个不同视图:
Hello, currently I am trying to build an application which has a ContentControl which switches between a derived DataGrid and a ListBox to provide two different views on Data:
<ContentControl Content="{Binding ElevationsView}" x:Name="ElevationsContentControl">
<ContentControl.Resources>
<DataTemplate x:Key="ElevationsDetailedList">
<local:ExtendedDataGrid ItemsSource="{Binding}" ScrollViewer.ScrollChanged="ElevationsDetailedList_ScrollChanged" AutoGenerateColumns="True" />
</DataTemplate>
<DataTemplate x:Key="ElevationsTileView">
<ListBox ItemsSource="{Binding}" ScrollViewer.ScrollChanged="ListBox_ScrollChanged" />
</DataTemplate>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="{x:Type ContentControl}">
<Style.Triggers>
<DataTrigger Binding="{Binding ViewMode}" Value="{x:Static local:ViewMode.DetailedList}">
<Setter Property="ContentTemplate" Value="{StaticResource ElevationsDetailedList}" />
</DataTrigger>
</Style.Triggers>
<Setter Property="ContentTemplate" Value="{StaticResource ElevationsTileView}" />
</Style>
</ContentControl.Style>
</ContentControl>
ExtendedDataGrid目前仅是DataGrid的派生类,没有任何其他功能.但是,如果我启动应用程序,并且初始ViewMode为"DetailedList",则会收到错误"Type of ExtendedDataGrid"的对象. 无法转换为类型"System.Windows.Controls.ListBox".
The ExtendedDataGrid is at the moment only a derived class from DataGrid without any additional functionality. But if I start the application and the initial ViewMode is "DetailedList", I get the error "An object of Type "ExtendedDataGrid" cannot be converted into the type "System.Windows.Controls.ListBox".
如果我从ExtendedDataGrid或ListView中删除ScrollChanged事件,或者当我将ExtendedDataGrid更改为DataGrid时,它将起作用.但是对于我来说,这都不是可接受的解决方案,因为我需要ExtendedDataGrid和ScrollChanged事件.
It works, if I delete the ScrollChanged Event from either the ExtendedDataGrid or the ListView, or when I change the ExtendedDataGrid to DataGrid. But both aren't acceptable solutions for me, as I need the ExtendedDataGrid and the ScrollChanged event.
有人知道原因吗,或者可以给我解决方法?
Does somebody knows the cause of this or can give me a workaround?
推荐答案
<ContentControl.Resources>
<DataTemplate x:Key="ElevationsDetailedList">
<local:ExtendedDataGrid ItemsSource="{Binding}" Loaded="ExtendedDataGrid_Loaded" AutoGenerateColumns="True" />
</DataTemplate>
<DataTemplate x:Key="ElevationsTileView">
<ListBox ItemsSource="{Binding}" Loaded="ListBox_Loaded" />
</DataTemplate>
</ContentControl.Resources>
private void ListBox_Loaded(object sender, RoutedEventArgs e)
{
ListBox listBox = sender as ListBox;
listBox.AddHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler(ListBox_ScrollChanged));
}
private void ExtendedDataGrid_Loaded(object sender, RoutedEventArgs e)
{
ExtendedDataGrid grid = sender as ExtendedDataGrid;
grid.AddHandler(ScrollViewer.ScrollChangedEvent, new RoutedEventHandler(ExtendedDataGrid_Loaded));
}
这应该满足XAML解析器:)
希望有帮助.
This should satisfy the XAML parser :)
Hope that helps.
请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
这篇关于在ContentControl中的两个控件之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!