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>
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
Related
In an WPF ListView I have ListViewItems that consist of two columns, one column is a TextBlock and the another one contains two buttons (Edit and Delete) in the same column.
Also the first row of the WPF listView contains a button for adding new ListViewItems to the ListView. This first row is only for this purpose.
I have followed the example explained here. I have adapted it to my model but it is exactly the same.
Below the DataTemplate defined in the resources and later applied to the ListView:
<DataTemplate x:Key="FirstRowTemplate1">
<Grid Width="190">
<Button Content="Add" Command="{Binding Path=DataContext.AddCommand, ElementName=MyListView}"
Width="180"
HorizontalAlignment="Center"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="NextRowTemplate1">
<TextBlock Text="{Binding PathString}"/>
</DataTemplate>
<DataTemplate x:Key="NameColumnTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate1}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate1}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<DataTemplate x:Key="FirstRowTemplate2"/>
<DataTemplate x:Key="NextRowTemplate2">
<StackPanel Orientation="Horizontal">
<Button Content="Edit" Command="{Binding Path=DataContext.EditCommand, ElementName=MyListView}" CommandParameter="{Binding PathString}"/>
<Button Content="-" Command="{Binding Path=DataContext.DelCommand, ElementName=MyListView}" Margin="10 0 0 0" CommandParameter="{Binding PathString}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="AddDelItemTemplate">
<ContentPresenter x:Name="ButtonPresenter"
Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
ContentTemplate="{StaticResource NextRowTemplate2}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Add}" Value="True">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource FirstRowTemplate2}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Add}" Value="False">
<Setter TargetName="ButtonPresenter"
Property="ContentTemplate"
Value="{StaticResource NextRowTemplate2}"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Then they are applied to the ListView:
<ListView x:Name="MyListView"
ItemsSource="{Binding pathList}"
IsSynchronizedWithCurrentItem="True">
<ListView.View>
<GridView>
<GridViewColumn Header="Paths" Width="350" CellTemplate="{StaticResource NameColumnTemplate}"/>
<GridViewColumn Width="70" Header="Edit/Delete" CellTemplate="{StaticResource AddDelItemTemplate}"/>
</GridView>
</ListView.View>
</ListView>
Now I am trying to perform an inline edit for the TextBlock once "Edit" button is clicked for this ListViewItem and exit edition mode once user validates it by pressing return key. If user press ESC key on edit mode, it should be exit edit mode and keep the value previous editing.How can I do this?
In my WPF MVVM project I use INotifyDataErrorinfo to handle validation within a DataGrid. I can successfully style error cells in my "Operator" column thus:
<DataGridTemplateColumn Header="Operator" Width="140">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Operator}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding OperatorId, Converter={StaticResource IsOperatorIdNullConverter}}" Value="False">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}">
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}" DisplayMemberPath="ErrorContent"/>
</ToolTip>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Salmon"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<controls:AutoCompleteBox Text="{Binding Operator, UpdateSourceTrigger=LostFocus, Mode=TwoWay}"
ItemsSource="{Binding Path=Data.OperatorNames, Source={StaticResource proxy}}"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="3"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
In my "OperatorType" column however this same technique doesn't work. Errors are being detected and the system default error styling is shown but my custom styling isn't. The code is:
<DataGridTemplateColumn Header="Operator type" Width="140">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding OperatorType.OperatorTypeName}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip DataContext="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget}">
<ItemsControl ItemsSource="{Binding Path=(Validation.Errors)}" DisplayMemberPath="ErrorContent"/>
</ToolTip>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="Salmon"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<Grid>
<controls:AutoCompleteBox ItemsSource="{Binding Path=Data.OperatorTypeNames, Source={StaticResource proxy}}"
ItemTemplate="{StaticResource AutoCompleteBoxItemOperatorTypeTemplate}"
SelectedItem="{Binding OperatorType, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
ValueMemberPath="OperatorTypeName"
IsTextCompletionEnabled="True"
FilterMode="Contains"
MinimumPrefixLength="3"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
The only differences as far as I can see are that:
The Text in "Operator" is using bound to a POCO (Operator) while for "OperatorType" it is bound to a POCO property (OperatorType.OperatorTypeName)
The AutoCompleteBox declarations are slightly different
I've tried numerous settings for the ToolTip DataContext but nothing seems to work.
Question
What do I need to change to get the "OperatorType" customised error styling to work?
Well, it was a bit of a journey, but the solution was to set a DataContext on the TextBlock:
<TextBlock DataContext="{Binding OperatorType}" Text="{Binding OperatorTypeName}">
This causes the Trigger to be pointed at the OperatorType POCO whereas the Text in the box is taken from OperatorType.OperatorTypeName.
I have DataGrid:
<DataGrid x:Name="PART_DataGrid"
ItemsSource="{TemplateBinding Items}"
AutoGenerateColumns="False"
Margin="2,25,2,2">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
FontSize="16"
IsEnabled="False">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=test }" Value="">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Translation" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="TranslationValueTB"
Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
BorderThickness="0"
FontSize="16" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
And I want to set that first column ComboBox will be enabled when second TextBox.Text is empty, I have tried different binding methods but none of them have worked. So how can I do that?
Bind to the Value source property that the TextBox in the second column is bound to:
<DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
FontSize="16"
IsEnabled="False">
<ComboBox.Style>
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Value}" Value="{x:Null}">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding Value}" Value="">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
Also make sure that the class where the Value property is defined implements the INotifyPropertyChanged interface and raises the PropertyChanged event in the setter of the Value property.
Remove the IsEnabled = false from combobox and do the same in the setter as follows, and do the binding as like follows,
<ComboBox.Style>
<Style TargetType="ComboBox">
<Setter Property="IsEnabled" Value="False"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Name }" Value="{x:Static sys:String.Empty}">
<Setter Property="IsEnabled" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
Item.Name - > this is the property you need to check
I have two collections.
The 'OuterCollection' collection contains the 'InnerCollection' collection and a property called 'OuterProperty'. In my 'InnerCollection', it only contains one property called 'InnerProperty'.
Now my ViewModel contains 1 'OuterCollection'. And it's set to the data context of the control. So a very easy set up.
ViewModel
{
OuterCollection;
}
OuterCollection
{
InnerCollection;
OuterProperty;
}
InnerCollection
{
InnerProperty;
}
Now in the code, I want to be able to access my 'OuterProperty' from a trigger within the context of my 'InnerCollection' How can I do this? And if I can't do this, is there an alternative way to accomplish this?
Here's my attempt:
<ListView ItemsSource="{Binding Path=OuterCollection}" />
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Path=InnerCollection}">
<ListBox.ItemTemplate>
<DataTemplate>
<Image>
<Image.Style>
<Style TargetType="Image">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=InnerProperty}" Value="blah" />
<Condition Binding="{Binding Path=DataContext.OuterCollection.OuterProperty, RelativeSource ={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Converter={StaticResource IsStringNullOrEmpty}}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="ToolTip">
<Setter.Value>
<TextBlock Text="test"/>
</Setter.Value>
</Setter>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
I have array of class Person in ViewModel and I want to show their names in table. I have also column with checkboxes. This is my View part:
<Grid>
<Grid.Resources>
<Style x:Key="CheckBoxStyle" TargetType="{x:Type Control}">
<Setter Property="Visibility" Value="Visible"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsSelectionAllowed}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Resources>
<ListView ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="40">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Style="{StaticResource CheckBoxStyle}"
.........................
.... some logic here ....
......................./>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="140"
Header="Number"
DisplayMemberBinding="{Binding Path=Name}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
I want to show/hide checkboxes according to value of IsSelectionAllowed boolean variable. Why DataTrigger is not firing?
Assuming IsSelectionAllowed is a property on the view model set on the data context, you will need a relative source binding - hopefully this is correct without any testing:
<DataTrigger Binding="{Binding Path=IsSelectionAllowed, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}" Value="False">
<Setter Property="Visibility" Value="Hidden"/>
</DataTrigger>
The binding of IsSelectionAllowed in your code is to the Person type.