2013-10-16 169 views
0

完全做到這是我的XAML參與WPF綁定...要使用XAML

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; 
    } 
ListView

<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; 

在這裏,我想僅顯示那些IsDuplicate設置爲true的收集項的綁定。我相信DataTemplate.Triggers是要走的路,但我無法弄清楚使其工作的確切語法。有什麼建議麼?

+1

我沒有得到它..只是爲了澄清你只想顯示那些ComponentParameter在你ItemsControl它有IsDuplicate爲真? – Nitin

+0

@Omribitan感謝您的鏈接,我會研究它。 – Sike12

+0

@nit正好...感謝您的時間 – Sike12

回答

1

這裏是您的ItemsControlItemContainerStyle用於將具有重複項目的項目隱藏爲假。

<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> 
+0

謝謝@nit。只是一個補充。如果沒有重複項,有沒有辦法顯示ComponentID和Name Binding? – Sike12

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> 
+0

感謝Omribitan。將看看它 – Sike12