基于某些条件的WPF BIndings ......完全使用Xaml完成

时间:2013-10-16 10:17:52

标签: c# .net wpf xaml wpf-controls

以下是我ListView

的Xaml
  <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是要走的路,但我无法弄清楚确切的语法。有什么建议?

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

以下是您ItemContainerStyle的{​​{1}}隐藏重复项为false的项目。

ItemsControl