如何绑定到WPF中的2个元素

时间:2017-01-20 08:38:00

标签: c# wpf binding

如何将Visibility元素的Grid绑定到两个Checkbox元素。如果至少检查其中一个网格,我希望显示网格。 我不想使用转换器。

这就是我所拥有的:

  /// <summary>
  /// Identifies whether the tags will be copied from source to target host
  /// </summary>
  private bool pasteTags = true;
  public bool PasteTags
  {
     get { return pasteTags; }
     set
     {
        pasteTags = value;
        RaisePropertyChanged("PasteTags");
     }
  }


  /// <summary>
  /// Identifies whether the dimensions will be copied from source to target host
  /// </summary>
  private bool pasteDimensions = true;
  public bool PasteDimensions
  {
     get { return pasteDimensions; }
     set
     {
        pasteDimensions = value;
        RaisePropertyChanged("PasteDimensions");
     }
  }


  /// <summary>
  /// Identifies if the views data grid is displayed or not.
  /// </summary>      
  public System.Windows.Visibility IsViewGridVisible
  {
     get
     {
        if (pasteDimensions || pasteTags)
           return System.Windows.Visibility.Visible;

        return System.Windows.Visibility.Collapsed;
     }
  }

XAML:

<Grid Visibility="{Binding IsViewGridVisible}"> </Grid>

2 个答案:

答案 0 :(得分:1)

您唯一需要的是在RaisePropertyChanged("IsViewGridVisible");PasteDimensions两个属性集中调用PasteTags

public bool PasteDimensions
  {
     get { return pasteDimensions; }
     set
     {
        pasteDimensions = value;
        RaisePropertyChanged("PasteDimensions");
        RaisePropertyChanged("IsViewGridVisible");
     }
  }

这是必需的,因为IsViewGridVisible取决于这些属性的两个值,并且需要告知WPF有关更改(它无法以任何其他方式解决)。

答案 1 :(得分:1)

您实际上并不需要IsViewGridVisible属性。

相反,您可以定义一个样式,在VisibilityMultiDataTrigger属性上按PasteTags设置网格的PasteDimensions

<Grid>
    <Grid.Style>
        <Style TargetType="Grid">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding PasteTags}" Value="True"/>
                        <Condition Binding="{Binding PasteDimensions}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="Visibility" Value="Visible"/>
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Style>
</Grid>