Я борюсь с привязкой источника данных к свойству текстового блока.Я добавил рабочий пример Datatrigger в TextBox.Проблема в TextBlock DataTrigger Я не могу получить работу.При запуске IsNameActive имеет значение «false», а BackGround имеет значение PaleVioletRed, но оно не изменяется при изменении IsNameActive.
public bool IsNameActive
{
get => !string.IsNullOrEmpty(FirstName);
}
public string FirstName
{
get => _firstName;
set
{
if (value != _firstName)
{
_firstName = value;
OnPropertyChanged(FirstName);
OnPropertyChanged(IsNameActive);
}
}
}
<TextBox
Grid.Row="0"
Grid.Column="1"
Margin="15"
Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Background" Value="RosyBrown" />
<Style.Triggers>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="aa">
<Setter Property="Background" Value="DarkOliveGreen" />
</DataTrigger>
<DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Self}}" Value="">
<Setter Property="Background" Value="CornflowerBlue" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<TextBlock
Grid.Row="2"
Grid.Column="1"
Margin="15"
Text="TextBlock with binding">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=DataContext.IsNameActive, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="Background" Value="CornflowerBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=DataContext.IsNameActive, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged}" Value="false">
<Setter Property="Background" Value="PaleVioletRed" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>