Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTrigger for Visibility of a control inside a DataTemplate not working

I have a ListBox which is binded to a List and it has a DataTemplate for it's items. Everything in DataTemplate works good except for visibility of second TextBlock! I don't understand what I'm doing wrong and I don't want to use converter, I've checked these links already:

Bind Bool to Visibility of TextBlock within a ListBox

Binding a Button's visibility to a bool value in ViewModel

<ListBox Name="lsb_Jobs" Grid.Column="3" Grid.Row="2" Grid.RowSpan="6" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                     BorderThickness="0,1,0,0" Padding="0,5" Margin="0,10,5,5">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid Height="45">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="25"/>
                                <ColumnDefinition Width="250"/>
                                <ColumnDefinition Width="100"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Grid.Style>
                                <Style TargetType="Grid">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Importance}" Value="0">
                                            <Setter Property="Background" Value="LimeGreen"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Importance}" Value=".25">
                                            <Setter Property="Background" Value="NavajoWhite"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Importance}" Value=".5">
                                            <Setter Property="Background" Value="Gold"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Importance}" Value=".75">
                                            <Setter Property="Background" Value="Orange"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Importance}" Value="1">
                                            <Setter Property="Background" Value="OrangeRed"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </Grid.Style>
                            <CheckBox Name="chb_IsDone" IsChecked="{Binding Done}" FlowDirection="LeftToRight" Checked="job_Done_Checked" Unchecked="job_Done_Checked"/>
                            <TextBlock Text="{Binding Subject}" Grid.Column="1" Foreground="Black" VerticalAlignment="Center" FontSize="14"/>
                            <TextBlock Text="Done" Grid.Column="3" HorizontalAlignment="Right" VerticalAlignment="Center" Visibility="Hidden" Margin="0,0,170,0">
                                <TextBlock.Style>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Done}" Value="True">
                                                <Setter Property="Visibility" Value="Visible"/>
                                            </DataTrigger>  
                                        </Style.Triggers>
                                    </Style>
                                </TextBlock.Style>
                            </TextBlock>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Can you tell me why it's not working?! I did exactly the same thing that I did for other controls! They work but TextBlock does not get visible! Is there any problem with Visibility property of TextBlock!? I tried FrameworkElement.Visibility already but that does not work either

like image 845
Saman Hakimzadeh Abyaneh Avatar asked Dec 11 '15 16:12

Saman Hakimzadeh Abyaneh


1 Answers

The Visibility="Hidden" that you explicitly set on the TextBlock is overriding whatever the Style does. Style is applied first, then finally explicit assignments in the tag attributes are applied. This makes sense: If you have a global TextBlock style and you set properties on an individual TextBlock, you want the global values for those properties to be overridden.

like image 161
15ee8f99-57ff-4f92-890c-b56153 Avatar answered Oct 23 '22 06:10

15ee8f99-57ff-4f92-890c-b56153