我想在许多列表视图中使用相同的样式。在我的风格中,我也定义了gridview列。
但是当我尝试跑步时,它会引发异常:
视图不能由多个人共享 的ListView。
我该如何解决这个问题?
XAML:
<Style x:Key="articleList" TargetType="{x:Type ListView}">
<Setter Property="VirtualizingStackPanel.IsVirtualizing" Value="True"/>
<Setter Property="ScrollViewer.IsDeferredScrollingEnabled" Value="True"/>
<Setter Property="ListView.ItemsSource" Value="{Binding}"/>
<Setter Property="ListView.View">
<Setter.Value>
<GridView>
<GridViewColumn Header="Subject" Width="300">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Subject}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Size" Width="75">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SizeFormatted}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Poster" Width="175">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Poster}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Age" Width="75">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding AgeFormatted}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</Setter.Value>
</Setter>
答案 0 :(得分:22)
将x:Shared property添加到GridView资源。在这个例子中查看GridView资源。
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Window1"
x:Name="Window"
Title="Window1"
Width="640" Height="480">
<Window.Resources>
<GridView x:Key="ViewBase1" x:Shared="False">
<GridViewColumn Header="Blah1" Width="70"/>
<GridViewColumn Header="Blah2" Width="70"/>
<GridViewColumn Header="Blah3" Width="70"/>
</GridView>
</Window.Resources>
<Grid x:Name="LayoutRoot">
<ListView Margin="0,0,0,120" View="{DynamicResource ViewBase1}" />
<ListView Margin="272,0,91,120" View="{DynamicResource ViewBase1}" />
</Grid>
</Window>