检查是否有任何UserControl已打开

时间:2015-12-07 12:58:51

标签: c# wpf combobox user-controls

我创建了UserControl,扩展了ComboBox的基本功能。

<ComboBox
    x:Name="MultiSelectCombo"  
    SnapsToDevicePixels="True"
    OverridesDefaultStyle="True"
    ScrollViewer.HorizontalScrollBarVisibility="Hidden"
    ScrollViewer.VerticalScrollBarVisibility="Auto"
    ScrollViewer.CanContentScroll="True"
    IsSynchronizedWithCurrentItem="True"
    StaysOpenOnEdit="True"
    Background="#61596f"
>
...
</ComboBox>

在页面上我使用3个控件:

<controls:MultiSelectComboBox SelectedItems="{Binding SelectedCategories, Mode=TwoWay}" Grid.Column="0" Grid.Row="0" x:Name="CategoriesFilter" DefaultText="Category" ItemsSource="{Binding Categories }" Style="{StaticResource FiltersDropDowns}"/>

<controls:MultiSelectComboBox SelectedItems="{Binding SelectedTypes, Mode=TwoWay}" Grid.Column="1" Grid.Row="0" x:Name="TypesFilter" DefaultText="Type" ItemsSource="{Binding Types, Mode=TwoWay}" Style="{StaticResource FiltersDropDowns}"/>

<controls:MultiSelectComboBox SelectedItems="{Binding SelectedBrandsList, Mode=TwoWay}"  Grid.Column="2" Grid.Row="0" x:Name="BrandsFilter" DefaultText="Brand" ItemsSource="{Binding BrandsList}" Style="{StaticResource FiltersDropDowns}"/>

当任何组合框打开时,我需要在此页面上更改网格元素Overlay的可见性。

我该怎么办?按名称检查每个项目会产生太多代码。 我尝试了这个,但无法获得我控制的子项目

<Grid.Style>
            <Style TargetType="Grid">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsChecked, ElementName=ComboBoxToggleButton}" Value="True">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Style>

其中ComboBoxToggleButton是打开/关闭下拉列表的组合框的按钮

1 个答案:

答案 0 :(得分:0)

我将使用MultiBinding

<Grid>
     <Grid.Visibility>
          <MultiBinding Mode="OneWay" Converter={StaticResource OpenToVisibilityConverter}>
              <Binding ElementName="CategoriesFilter" Path="[a Open property on your UserControl]" />
              <Binding ElementName="TypesFilter" Path="[a Open property on your UserControl]" />
              <Binding ElementName="BrandsFilter" Path="[a Open property on your UserControl]" />TypesFilter
          </MultiBinding>
     </Grid.Visibility>
</Grid>

OpenToVisibilityConverter是一个IMultiValueConverter,它检查每个Binded控件的isopen属性并返回正确的Visibility。

似乎没有太多代码可以使用。