Question

I'm trying to change the text of a textblock based on a datatrigger. The datatrigger looks like this:

    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty}" Value="true">
            <Setter Property="Text" Value="{Binding Path=Name}" />
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty}" Value="false">
            <Setter Property="Text" Value="{Binding Path=NameWithoutExtension}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

This code is found within the xaml file of the class "MyView" The cs file of this class contains the following code:

public static readonly DependencyProperty ShowFileExtensionsProperty =
DependencyProperty.Register("ShowFileExtensions", typeof(bool), typeof(MyView), new UIPropertyMetadata(false));

public bool ShowFileExtensions
{
    get { return (bool)GetValue(ShowFileExtensionsProperty); }
    set
    {
        SetValue(ShowFileExtensionsProperty, value);
        updateViewCollection();
    }
}

The textblock is displayed within a listview, that has been bound to a list of objects, these objects contain the property "Name" and "NameWithoutExtension".

The problem is that the text in the textblock always stays blank. Somehow the datatrigger is never triggered. Any idea what is wrong with the datatriggers?

PS: The full code follows

<UserControl x:Class="com.xploreplus.Filedrops.explorer.listview.FiledropsFileList"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:l="clr-namespace:com.xploreplus.Filedrops.explorer.listview.views"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
    <Style x:Key="Extension" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="true">
                <Setter Property="Text" Value="brol2" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=ShowFileExtensionsProperty, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="false">
                <Setter Property="Text" Value="blabla" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

</UserControl.Resources>

<Grid>
    <ListView Name="viewComponent">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Width="16" Height="16" Source="{Binding Path=Icon}" />
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Name">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Style="{StaticResource Extension}" />
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="Extension" DisplayMemberBinding="{Binding Path=Extension}" />
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

Was it helpful?

Solution

As the textblocks are shown in listview then the DataContext of textblock will the ItemDataContext if not set explicitly. That is why your bindings are not working. You will have to provide the relativesource to binding

Try updating the bindings like

<DataTrigger Binding="{Binding Path=ShowFileExtensions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" Value="true">

used {x:Type Control} assuming this is the type of your view else you will have to change to your view type.

Hope this helps

Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top