0

这是我的 XAML

 <ListView x:Name="missingVariablesListView" ScrollViewer.CanContentScroll="True" HorizontalAlignment="Left" Height="320" Margin="81,28,0,0" VerticalAlignment="Top" Width="641" ItemsSource="{Binding}">
                <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}"/>
                                                <TextBlock Text="   "/>                                           
                                                <TextBlock Text="Variable Value: " Foreground="Blue"/>
                                                <TextBlock Text="{Binding Value}"/>
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                                <ItemsControl.ItemContainerStyle>
                                    <Style >
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding IsMissing}" Value="false">
                                                <Setter Property="UIElement.Visibility" Value="Collapsed"/>                                                    
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ItemsControl.ItemContainerStyle>
                            </ItemsControl>                           
                                <TextBlock Text="-----------------------------------------------------------------"/>
                        </StackPanel>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

这是背后的代码

  this.missingVariablesListView.DataContext = //Code to fill in the list View

涉及的课程

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 IsMissing
   {
    get;set;
   }

样本输出(目前)

ComponentID: Component1
--------------------------

ComponentID: Component2
VariableName:Var1 Variable Value:Val1
VariableName:Var2 Variable Value:Val2
-----------------------

ComponentID: Component3
-----------------------

ComponentID: Component4
-----------------------

我想要做的是,每当 itemsControl 中的内部元素的布尔 IsMissing 为真时,我想确保 StackPanel 中的 ComponentID 和 Name 属性(方向为水平)不显示在窗口中,包括子元素。基本上,我试图找到一种方法来排除 isMissing 变量设置为 true 的特定 ComponentID 的整个描述。对此有何建议?

4

3 回答 3

1

您可以使用 aBooleanToVisibilityConverter为您隐藏这些项目,但您可能需要为此更改属性或添加新IsPresent属性:

Resources

<Converters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" 
    IsInverted="True" />

在你的ListView.ItemTemplate

<StackPanel Orientation="Horizontal" Visibility="{Binding IsMissing, Converter={
    StaticResource BoolToVisibilityConverter}}">
    <TextBlock Text="ComponentID: " FontWeight="Bold" Foreground="Brown" />
    <TextBlock Text="{Binding Name}"/>
</StackPanel>

自定义BoolToVisibilityConverter属性IsInverted

[ValueConversion(typeof(bool), typeof(Visibility))]
public class BoolToVisibilityConverter : IValueConverter
{
    public bool IsInverted { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || value.GetType() != typeof(bool)) return null;
        bool boolValue = IsInverted ? !(bool)value : (bool)value;
        return boolValue ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null || value.GetType() != typeof(Visibility)) return null;
        if (IsInverted) return (Visibility)value != Visibility.Visible;
        return (Visibility)value == Visibility.Visible;
    }
}

现在只需一分钟即可实现它。我已经更新了上面的 XAML 示例以使用它。

于 2013-10-21T11:54:58.030 回答
1

我会考虑IsAnyParameterMissingComponent类中添加一个属性:

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 bool IsAnyParameterMissing
    {
        get { return this.Parameters.Any(param => param.IsMissing); }
    }
}

然后将可见性绑定到此属性:

<StackPanel Orientation="Horizontal" Visibility="{Binding IsAnyParameterMissing, Converter={BooleanToVisibilityConverter}}">
    <TextBlock Text="ComponentID: " FontWeight="Bold" Foreground="Brown" />
    <TextBlock Text="{Binding Name}"/>
</StackPanel>

这只会显示集合中的StackPanel任何s 的属性等于。请注意,如果任何项目的属性发生更改,这不会更改可见性!这将需要一些额外的工作。ParameterIsMissingtrueIsMissing

编辑:请注意,{BooleanToVisibilityConverter}可能需要调整,具体取决于您可用的转换器。不过应该是一个简单的任务。

于 2013-10-21T12:09:15.480 回答
1

我注意到的一件事是您没有将属性(例如“ isMissing ”)实现为DependencyProperty您也不会通过INotifyPropertyChanged使用属性更改通知。

您必须实现两者中的任何一个,否则属性的更改将不会通过绑定传播。也就是说,触发器不会触发...

于 2013-10-21T11:57:58.433 回答