2016-02-21 51 views
1

请问,我如何设置在一个DataTrigger分组ListView中的虚假扩展器并评估ListView中的任何列值?如何在ListView中不扩展分组?

我的代码:

XAML:

<ListView Name="lvwGrupoPunto"> 
    <ListView.View> 
     <GridView> 
      <GridView.ColumnHeaderContainerStyle> 
       <Style TargetType="GridViewColumnHeader"> 
        <Setter Property="Visibility" Value="Collapsed" /> 
       </Style> 
      </GridView.ColumnHeaderContainerStyle> 
      <GridViewColumn Header="Prestación" Width="300" DisplayMemberBinding="{Binding NombrePrestacion}"/> 
      <GridViewColumn Header="Tipo Valor Punto" Width="30" DisplayMemberBinding="{Binding NombreTipoValorPunto}"/> 
      <GridViewColumn Header="Valor Punto" Width="50" DisplayMemberBinding="{Binding ValorCalculoPunto, StringFormat=N2}"/> 
     </GridView> 
    </ListView.View> 
    <ListView.GroupStyle> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate> 
           <Expander> 
            <Expander.Style> 
             <Style TargetType="Expander"> 
              <Style.Triggers> 
               <DataTrigger Binding="{Binding Path=ValorCalculoPunto}" Value="{x:Null}"> 
                <Setter Property="IsExpanded" Value="False" /> 
               </DataTrigger> 
              </Style.Triggers> 
             </Style> 
            </Expander.Style> 
            <Expander.Header> 
             <StackPanel Orientation="Horizontal"> 
              <TextBlock Text="{Binding Name}" FontWeight="Bold"/> 
             </StackPanel> 
            </Expander.Header> 
            <ItemsPresenter /> 
           </Expander> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </GroupStyle.ContainerStyle> 
     </GroupStyle> 
    </ListView.GroupStyle> 
</ListView> 

我的C#:

public class ItemValorPunto 
    { 
     public string NombrePrestacion { get; set; } 
     public string NombreGrupoPunto { get; set; }  
     public decimal? ValorCalculoPunto { get; set; } 
     public string TipoValorPunto { get; set; } 
    } 
    List<ItemValorPunto> itemValorPuntoColeccion = new List<ItemValorPunto>(); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 1", ValorCalculoPunto = null, TipoValorPunto = "" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SCL 70", NombreGrupoPunto = "PARTO 2", ValorCalculoPunto = 3.4000, TipoValorPunto = "$" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SM-RNP", NombreGrupoPunto = "PARTO 2", ValorCalculoPunto = 2.5000, TipoValorPunto = "%" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 3", ValorCalculoPunto = null, TipoValorPunto = "" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "", NombreGrupoPunto = "PARTO 4", ValorCalculoPunto = null, TipoValorPunto = "" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI MITOCONDRIALE", NombreGrupoPunto = "PARTO 5", ValorCalculoPunto = 10.00, TipoValorPunto = "$" }); 
    itemValorPuntoColeccion.Add(new ItemValorPunto() { NombrePrestacion = "ANTICUERPOS ANTI SM-RNP", NombreGrupoPunto = "PARTO 5", ValorCalculoPunto = 0.10, TipoValorPunto = "%" }); 
    lvwGrupoPunto.ItemsSource = itemValorPuntoColeccion; 
    CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lvwGrupoPunto.ItemsSource); 
    PropertyGroupDescription groupDescription = new PropertyGroupDescription("NombreGrupoPunto"); 
    view.GroupDescriptions.Add(groupDescription); 

我只需要表现出与非空 “ValorCalculoPunto” 项目

But I get this

回答

0

在XAML更改Expander此:

<Expander IsExpanded="{Binding Items[0].ValorCalculoPunto, Converter={StaticResource IsNotNullConverter}, Mode=OneWay}"> 
    <Expander.Header> 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Name}" FontWeight="Bold"/> 
     </StackPanel> 
    </Expander.Header> 
    <ItemsPresenter /> 
</Expander> 

IsNotNullConverter被添加到WindowsResources集合。

<Window.Resources> 
    <local:IsNotNullConverter x:Key="IsNotNullConverter"></local:IsNotNullConverter> 
</Window.Resources> 

及其实施是:

public class IsNotNullConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     return value != null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

的问题是,默认情况下ExpanderIsExpanded属性为false。所以你不能只有一个DataTrigger告诉何时Expander应该关闭。它们默认关闭。

另外,当您绑定DataContext中的一个组是CollectionViewGroup,因此Items[0]部分处于绑定状态。

+0

是的我忘了设置IsExpandable = True,但所有的项目都是可见的。非常感谢,它完美的工作,你拯救了生命。 –