'视图不能由多个ListView共享'Wpf错误

时间:2010-03-10 10:20:05

标签: wpf gridview listview

这段代码有什么问题?它抛出异常:“View不能由多个ListView共享”

<ListView 
    ItemsSource="{Binding}"  
    SelectionMode="Extended">
 <ListView.Style>

  <Style TargetType="ListView">   
   <Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="True">
     <Setter Property="View" Value="{StaticResource GridViewCompanies}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="False">
     <Setter Property="View" Value="{StaticResource GridViewPeople}" />
    </DataTrigger>
    <DataTrigger Binding="{Binding ElementName=MyControl, Path=IsCompany}" Value="{x:Null}">
     <Setter Property="View" Value="{StaticResource GridViewBoth}" />
    </DataTrigger>
   </Style.Triggers>
  </Style>

 </ListView.Style>          
</ListView>

public bool? IsCompany
{
 get { return (bool?)GetValue(IsCompanyProperty); }
 set { SetValue(IsCompanyProperty, value); }
}        
public static readonly DependencyProperty IsCompanyProperty =
 DependencyProperty.Register("IsCompany", typeof(bool?), typeof(MyControl), new UIPropertyMetadata(null));

编辑:

我尝试在后面设置View代码并且它有效。那么XAML的问题是什么?

if() ..
MyListView.View = Resources["GridViewCompanies"] as GridView;

1 个答案:

答案 0 :(得分:0)

错误是因为GridView正在应用于多个ListView。您的ListView样式是否应用于多个控件?我看了一下Reflector,看起来这个场景应该适用于单个控件。

你到底想要完成什么?我想你只想展示不同的专栏。也许你可以创建一个附加的行为来为GridView生成列,具体取决于你绑定的属性的值。