我有一个ListView,其中View包含GridView和许多列定义。 如下:
<ListView Name="SampleListView" ItemsSource="{Binding SomeSource}" >
<ListView.View>
<GridView AllowsColumnReorder="False">
<GridViewColumn Header="{Binding ColumnHeader1,Mode=OneWay}" CellTemplate="{StaticResource Column1Template}" />
<GridViewColumn Header="{Binding ColumnHeader2,Mode=OneWay}" CellTemplate="{StaticResource Column2Template}" />
<GridViewColumn Header="{Binding ColumnHeader3,Mode=OneWay}" CellTemplate="{StaticResource Column3Template}" />
<GridViewColumn Header="{Binding ColumnHeader4,Mode=OneWay}" CellTemplate="{StaticResource Column4Template}" />
<GridViewColumn Header="{Binding ColumnHeader5,Mode=OneWay}" CellTemplate="{StaticResource Column5Template}" />
</GridView>
</ListView.View>
</ListView>
(这完美无缺)
我需要隐藏用户首选项的第一列,因此我创建了一个样式触发器来完成此操作。具有样式触发器的代码如下所示
<ListView Name="SampleListView" ItemsSource="{Binding SomeSource}" >
<ListView.Resources>
<Style x:Key="{x:Type ListView}" TargetType="ListView">
<Style.Triggers>
<DataTrigger Binding="{Binding Source={x:Static p:User.Default},Path=ShowColumn1,Mode=OneWay}" Value="False">
<Setter Property="View">
<Setter.Value>
<GridView x:Name="WithOutColumn1" AllowsColumnReorder="False">
<GridViewColumn Header="{Binding ColumnHeader2,Mode=OneWay}" CellTemplate="{StaticResource Column2Template}" />
<GridViewColumn Header="{Binding ColumnHeader3,Mode=OneWay}" CellTemplate="{StaticResource Column3Template}" />
<GridViewColumn Header="{Binding ColumnHeader4,Mode=OneWay}" CellTemplate="{StaticResource Column4Template}" />
<GridViewColumn Header="{Binding ColumnHeader5,Mode=OneWay}" CellTemplate="{StaticResource Column5Template}" />
</GridView>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Source={x:Static p:User.Default},Path=ShowColumn1,Mode=OneWay}" Value="True">
<Setter Property="View">
<Setter.Value>
<GridView x:Name="WithColumn1" AllowsColumnReorder="False">
<GridViewColumn Header="{Binding ColumnHeader1,Mode=OneWay}" CellTemplate="{StaticResource Column1Template}" />
<GridViewColumn Header="{Binding ColumnHeader2,Mode=OneWay}" CellTemplate="{StaticResource Column2Template}" />
<GridViewColumn Header="{Binding ColumnHeader3,Mode=OneWay}" CellTemplate="{StaticResource Column3Template}" />
<GridViewColumn Header="{Binding ColumnHeader4,Mode=OneWay}" CellTemplate="{StaticResource Column4Template}" />
<GridViewColumn Header="{Binding ColumnHeader5,Mode=OneWay}" CellTemplate="{StaticResource Column5Template}" />
</GridView>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
</ListView>
使用此样式时,GridViewColumn Header属性的Binding不起作用。
但CellTemplate的Binding确实正确显示。
VS中的输出窗口显示此错误
System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:路径= ColumnHeader1;的DataItem = NULL; target元素是'GridViewColumn'(HashCode = 56585823); target属性是'Header'(类型'Object
任何人都可以了解为何会发生这种情况以及如何解决这个问题?
尝试的事情:
答案 0 :(得分:3)
万一其他人遇到这样的问题我找到了解决方案。
GridViewColumn Header属性必须在该项目的XAML标记中设置和绑定,如下所示。
<Setter Property="View">
<Setter.Value>
<GridView x:Name="WithOutColumn1" AllowsColumnReorder="False">
<GridViewColumn CellTemplate="{StaticResource Column2Template}">
<GridViewColumnHeader Content="{Binding ColumnHeader2}" />
</GridViewColumn>
<GridViewColumn CellTemplate="{StaticResource Column3Template}">
<GridViewColumnHeader Content="{Binding ColumnHeader3}" />
</GridViewColumn>
<GridViewColumn CellTemplate="{StaticResource Column4Template}">
<GridViewColumnHeader Content="{Binding ColumnHeader3}" />
</GridViewColumn>
<GridViewColumn CellTemplate="{StaticResource Column5Template}">
<GridViewColumnHeader Content="{Binding ColumnHeader3}" />
</GridViewColumn>
</GridView>
</Setter.Value>
如果你遇到类似的问题,希望完全有用。