【问题标题】:'View cannot be shared by more than one ListView' Wpf Error'视图不能由多个 ListView 共享' Wpf 错误
【发布时间】:2010-03-10 10:20:05
【问题描述】:

这段代码有什么问题?它抛出异常:“视图不能由多个 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));

已编辑:

我尝试在后面的代码中设置视图,它可以工作。那么 XAML 有什么问题呢?

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

【问题讨论】:

    标签: wpf gridview listview


    【解决方案1】:

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

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

    【讨论】:

    • 不,我只有 1 个 ListView,我在其中直接定义了样式。所有的 StaticResources (GridViewBoth, ...) 都在 UserControl 的资源中定义。是的,你是对的,这正是我想要做的 - 根据属性显示不同的列。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2015-07-20
    相关资源
    最近更新 更多