如果选择了选项,则WPF样式隐藏TextBox

时间:2015-11-08 21:03:54

标签: wpf

如果在ComboBox中选择了一个选项,是否可以使用触发器隐藏ControlTemplate中的文本框?甚至转换,因为我想避免使用后面的代码。

这是我的Style,一旦用户在下拉菜单中选择了一个项目,TextBlock就是我要隐藏的对象

<!-- Combo Box Style -->
    <Style TargetType="ComboBox">
        <Style.Resources>
            <SolidColorBrush x:Key="colour1" Color="{Binding Source={StaticResource NorGrey}, Path=Color}" />
            <SolidColorBrush x:Key="colour2" Color="{Binding Source={StaticResource DarGrey}, Path=Color}" />
            <SolidColorBrush x:Key="colour3" Color="{Binding Source={StaticResource LigGrey}, Path=Color}" />

            <Style TargetType="ComboBoxItem">
                <Setter Property="SnapsToDevicePixels" Value="True" />
                <Setter Property="OverridesDefaultStyle" Value="True" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ComboBoxItem">
                            <Border x:Name="Border"
                                    Padding="4"
                                    SnapsToDevicePixels="True"
                                    Background="Transparent">
                                <ContentPresenter />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="Border" Property="Background" Value="{DynamicResource colour3}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <ControlTemplate x:Key="ComboBoxToggleButton" TargetType="ToggleButton">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition Width="20" />
                    </Grid.ColumnDefinitions>
                    <Border x:Name="Border"
                            Grid.ColumnSpan="2"
                            CornerRadius="5"
                            Background="{DynamicResource colour1}"/>
                    <Border Grid.Column="0"
                            CornerRadius="5,0,0,5"
                            Margin="1"
                            Background="{DynamicResource colour2}" />
                    <Path x:Name="Arrow"
                          Grid.Column="1"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Center"
                          Data="M 0 0 L 4 4 L 8 0 Z"
                          Fill="White" />
                </Grid>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Background" Value="{DynamicResource colour3}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>

        </Style.Resources>
        <Setter Property="Margin" Value="5" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
        <Setter Property="MinHeight" Value="20" />
        <Setter Property="MinWidth" Value="80" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ComboBox">
                    <Grid>
                        <ToggleButton x:Name="ToggleButton"
                                      Template="{StaticResource ComboBoxToggleButton}"
                                      Grid.Column="2"
                                      Focusable="False"
                                      ClickMode="Press"
                                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" />
                            <ContentPresenter x:Name="ContentSite"
                                          IsHitTestVisible="False"
                                          Content="{TemplateBinding SelectionBoxItem}"
                                          ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                          ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                          Margin="6,6,25,6"
                                          VerticalAlignment="Stretch"
                                          HorizontalAlignment="Left" />
                        <TextBlock x:Name="DefaultText"
                                 Foreground="Red"
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center"
                                 Margin="3,3,23,3"
                                 Background="Transparent"
                                 Visibility="Visible"
                                 Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Path=Text}" />
                        <TextBlock x:Name="NoItems"
                                 Foreground="White"
                                 HorizontalAlignment="Center"
                                 VerticalAlignment="Center"
                                 Margin="3,3,23,3"
                                 Background="Transparent"
                                 Visibility="Hidden"
                                 Text="-Empty-" />
                        <Popup x:Name="Popup"
                               Placement="Bottom"
                               IsOpen="{TemplateBinding IsDropDownOpen}"
                               AllowsTransparency="True"
                               Focusable="False"
                               PopupAnimation="Slide">
                            <Grid x:Name="DropDown"
                                  SnapsToDevicePixels="True"
                                  MinWidth="{TemplateBinding ActualWidth}"
                                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                                <Border x:Name="DropDownBorder"
                                        Background="{DynamicResource colour1}" />
                                <ScrollViewer Margin="4,6,4,6"
                                              SnapsToDevicePixels="True">
                                    <StackPanel IsItemsHost="True"
                                                KeyboardNavigation.DirectionalNavigation="Contained" />
                                </ScrollViewer>
                            </Grid>
                        </Popup>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="HasItems" Value="False">
                            <Setter TargetName="DropDownBorder" Property="MinHeight" Value="10" />
                            <Setter TargetName="NoItems" Property="Visibility" Value="Visible" />
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="True">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
                        </Trigger>
                        <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                            <Setter TargetName="DropDownBorder" Property="CornerRadius" Value="4" />
                            <Setter TargetName="DropDownBorder" Property="Margin" Value="0,2,0,0" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

1 个答案:

答案 0 :(得分:1)

使用转换为Spawn建议结束,这是转换器:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Test_Project.Converters
{
    public class ComboDefaultText : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if ((int)value >= 0)
            {
                return "Hidden";
            }
            return "Visible";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

这是绑定:

Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}},Path=SelectedIndex, Converter={StaticResource ComboDefaultText}}"