DataTrigger for Visibility of a control inside a DataTemplate not working
Asked Answered
C

1

11

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

Corrales answered 11/12, 2015 at 16:25 Comment(0)
P
14

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.

Pieria answered 11/12, 2015 at 16:32 Comment(1)
yep! That's what I was going to say :)Publicspirited

© 2022 - 2024 — McMap. All rights reserved.