【问题标题】:WPF BIndings based on certain condition... to be done entirely using Xaml基于特定条件的 WPF BIndings...完全使用 Xaml 完成
【发布时间】:2013-10-16 10:17:52
【问题描述】:

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

【问题讨论】:

  • 我不明白..只是为了澄清你只想在你的 ItemsControl 中显示那些 IsDuplicate 为 true 的 ComponentParameter?
  • @Omribitan 感谢您提供的链接,我会调查一下。
  • @nit 正是...感谢您的宝贵时间

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


【解决方案1】:

这里是 ItemContainerStyle 用于您的 ItemsControl 以隐藏重复为 false 的项目。

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

【讨论】:

  • 谢谢@nit。再加上一个。如果没有重复项,有没有办法不显示 ComponentID 和 Name Binding?
【解决方案2】:

如果要根据IsDuplicate 属性显示两个不同的DataTemplate,可以使用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>

【讨论】:

  • 感谢 Omribitan。会调查的
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 2017-02-10
  • 2010-10-20
  • 2011-08-04
  • 1970-01-01
  • 1970-01-01
  • 2018-11-27
  • 1970-01-01
相关资源
最近更新 更多