I have a GridViewColumn
that lists file paths. I have a filewatcher, and if the file is moved out of the watched directory, I want to strike through the filepath in my GridViewColumn
.
Here is my XAML for the GridViewColumn
:
<GridViewColumn x:Name="FileNameHeader" Header="File Name" DisplayMemberBinding="{Binding filename}" />
I can add a property to the structure that contains filename
such as FileExists()
which would return false
if the file is missing. Then, I just need a way to set the strikethrough text. Could I possibly use some sort of styling?
CodePudding user response:
You could add a property of type bool
e.g. Exists
to your file structure to indicate that a file exists or not. Do not forget to implement INotifyPropertyChanged
so that changes to the property are reflected in the user interface. In the following, I assume that your file structure is a class called FileData
. Make sure to change it to the real name in your project. I changed the property name filename
to Filename
. It is a good practice to adhere to common naming guidelines, see Capitalization Conventions for reference.
In order to apply strikethough to the text, you need to create a custom CellTemplate
, as there is no direct way to style the column. In the DataTemplate
you create a TextBlock
to display the Filename
. Add a style to the TextBlock
with a DataTrigger
that sets the Strikethrough
text decoration depending on the Exists
property of your file structure.
<GridViewColumn x:Name="FileNameHeader" Header="File Name">
<GridViewColumn.CellTemplate>
<DataTemplate DataType="{x:Type local:FileData}">
<TextBlock Text="{Binding Filename}">
<TextBlock.Style>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Exists}" Value="False">
<Setter Property="TextDecorations" Value="Strikethrough"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
You do not necessarily need to set the DataType
, but it helps with autocomplete in the editor.