0

这是我的 XamlListView

  <ListView x:Name="duplicateVarsInCompXMLListView" ItemsSource="{Binding}"  HorizontalAlignment="Left"  VerticalAlignment="Top" Width="306">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="ComponentID: " FontWeight="Bold" Foreground="Brown" />
                        <TextBlock Text="{Binding Name}"/>
                    </StackPanel>                                                                   
                    <ItemsControl ItemsSource="{Binding Parameters}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <StackPanel>                 
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Variable Name: " Foreground="Green"/>
                                        <TextBlock Text="{Binding Name}"/>
                                    </StackPanel>                                    
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Variable Value: " Foreground="Blue"/>
                                        <TextBlock Text="{Binding Value}"/>
                                    </StackPanel>
                                </StackPanel>                                
                            </DataTemplate>                               
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>                    
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

代码隐藏

        private ObservableCollection<Component> mastercomponentcollection;
        mastercomponentcollection = //code to populate the collection 
        duplicateVarsInCompXMLListView.DataContext = this.mastercomponentcollection;

涉及的课程

    public class Component
    {
    private ObservableCollection<ComponentParameter> parameters = new ObservableCollection<ComponentParameter>();
    public string Name
    {
        get;
        set;
    }

    public ObservableCollection<ComponentParameter> Parameters
    {
        get{return parameters;}
        set{parameters = value;}
    }
}


public class ComponentParameter
{
    public string Name
    {
        get;set;
    }

    public string Value
    {
        get;set;
    }

    public bool HasErrors
    {
        get;
        set;
    }

    public bool IsDuplicate
    {
        get; set;
    }

    public bool IsMissing
    {
        get;set;
    }

在这里,我只想显示那些IsDuplicate设置为 true 的集合项的绑定。我相信DataTemplate.Triggers这是要走的路,但我无法弄清楚使其工作的确切语法。有什么建议么?

4

2 回答 2

1

这是ItemContainerStyleItemsControl隐藏重复为假的项目的方法。

  <ItemsControl ItemsSource="{Binding Parameters}">
        <ItemsControl.ItemContainerStyle>
            <Style >
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsDuplicate}" Value="false">
                        <Setter Property="UIElement.Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </ItemsControl.ItemContainerStyle>
于 2013-10-16T10:39:21.703 回答
1

如果要DataTemplate根据IsDuplicate属性显示两个不同的,可以使用DataTemplateSelector

创建一个派生自DataTemplateSelector选择的类DataTemplate

public class MyTemplateSelector : DataTemplateSelector
{
      public DataTemplate FirstTemplate { get; set; }

      public DataTemplate SecondTemplate { get; set; }

      public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
      {
               var model = item as ComponentParameter;

               if (model.IsDuplicated)
                    return FirstTemplate;

              return SecondTemplate;
      }
}

在您的资源中创建它并在您的 xaml 中定义模板:

<local:MyTemplateSelector x:Key="itemTemplateSelector">
        <local:MyTemplateSelector.FirstTemplate>
            <DataTemplate>
                  <StackPanel>                 
                         <StackPanel Orientation="Horizontal">
                               <TextBlock Text="Variable Name: " Foreground="Green"/>
                               <TextBlock Text="{Binding Name}"/>
                          </StackPanel>                                    
                          <StackPanel Orientation="Horizontal">
                               <TextBlock Text="Variable Value: " Foreground="Blue"/>
                               <TextBlock Text="{Binding Value}"/>
                          </StackPanel>
                   </StackPanel>                                
             </DataTemplate>                  
        </local:MyTemplateSelector.FirstTemplate>
        <local:MyTemplateSelector.SecondTemplate>
            <DataTemplate>

                <!-- Implementation without bindings goes here -->

            </DataTemplate>
        </local:MyTemplateSelector.SecondTemplate>
</local:MyTemplateSelector>

并将其用于您的ListView

 <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="ComponentID: " FontWeight="Bold" Foreground="Brown" />
                    <TextBlock Text="{Binding Name}"/>
                </StackPanel>                                                                   
                <ItemsControl ItemsSource="{Binding Parameters}" ItemTemplateSelector="{StaticResource itemTemplateSelector}"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
于 2013-10-16T10:38:13.020 回答