Home > OS >  WPF How can set elements visibility?
WPF How can set elements visibility?

Time:12-26

When select the combobox item, want to change the visibility of the elements (Label, RadComboBox). If selected combobox item's value are "Month", set visible. else hidden. But it doesn't work. What's the problem?

Please, answer to me.

<Control.Template>
        <ControlTemplate TargetType="{x:Type Mods_UiMod_FrameControls:UiFrameContent}">
            <StackPanel x:Name="Part_MainStack" DataContext="{Binding}">
                <telerik:RadComboBox Grid.Row="3"
                                     Grid.Column="1"
                                     Height="20"
                                     Margin="5 0 5 0"
                                     DisplayMemberPath="CODE_NAME"
                                     FontSize="11"
                                     ItemsSource="{Binding Source=
                                     {x:Static Public_Code:CommonCodes.FormTypes}}"
                                     SelectedIndex="{Binding SelectedIndexForm,
                                     UpdateSourceTrigger=PropertyChanged}"
                                     SelectedValuePath="CODE_ID"
                                     SelectionBoxTemplate="{StaticResource CodeCBTemplate}"  
                                     x:Name="cbType"/>
                <Label Grid.Row="4"
                       Grid.Column="0"
                       Content="Create Month"
                       FontSize="11"
                       Foreground="White" Name="lbCreateMonth">
                       <Label.Style>
                           <Style TargetType="Label">
                               <Setter Property="Visibility" Value="Hidden"/>
                               <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=cbType, Path=SelectedValue}" Value="Month">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>
                               </Style.Triggers>
                           </Style>
                       </Label.Style>
                </Label>
                <telerik:RadComboBox Grid.Row="4"
                                     Grid.Column="1"
                                     Height="20"
                                     Margin="5 0 5 0"
                                     DisplayMemberPath="CODE_NAME"
                                     FontSize="11"
                                     ItemsSource="{Binding Source={x:Static Public_Code:CommonCodes.TimeMonthCodes}}"
                                     SelectedIndex="{Binding SelectedIndexCreateMonth,
                                                             UpdateSourceTrigger=PropertyChanged}"
                                     SelectedValuePath="CODE_ID"
                                     SelectionBoxTemplate="{StaticResource CodeCBTemplate}" Name="cbCreateMonth">
                    <telerik:RadComboBox.Style>
                        <Style TargetType="{x:Type telerik:RadComboBox}">
                            <Setter Property="Visibility" Value="Hidden"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding ElementName=cbType, Path=SelectedValue}" Value="Month">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </telerik:RadComboBox.Style>
                </telerik:RadComboBox>
            <StackPanel x:Name="Part_MainStack" DataContext="{Binding}">
    </ControlTemplate>
</Control.Template>

Visibility is set to Hidden in Label and RadComboBox.

When selecting ComboBox using DataTrigger, the visibility of Label and RadComboBox was changed to Visible. But it doesn't work.

CodePudding user response:

The “Element Visibility” Trigger fires when specified element(s) enters the viewport after scroll down. If the specified scroll depth is visible in the viewport when the page loads, the trigger will fire without a scroll occurring. Below you can find how to set up a box with “Element Visibility” trigger, all supported settings for this trigger explained as well as how to trigger it with the JavaScript Events API.

CodePudding user response:

Create a new variable Boolean let toBeVisible, its value to be update on the combobox selection. Use this values to make required element visibility.

 <Label Visibility="{Binding Path=LoadFilePath, Converter={StaticResource FalsyToCollapsedVisibilityConverter}}"/>

Also need to add the dependency in the top of your .xmal file like this

<Window
    xmlns:converters="clr-namespace:EBS.iCU.Converters"
     <Window.Resources>
            <ResourceDictionary>
                <converters:FalsyToCollapsedVisibilityConverter x:Key="FalsyToCollapsedVisibilityConverter" />
                <converters:FalsyToHiddenVisibilityConverter x:Key="FalsyToHiddenVisibilityConverter" />
            </ResourceDictionary>
        </Window.Resources>
  • Related