Tại sao đoạn mã sau nhận được lỗi runtime:Làm cách nào để có được trình kích hoạt để thay đổi màu của một TextBlock dựa trên một thuộc tính DataContext?
Members of the Triggers collection must be of type EventTrigger
Nhưng các yếu tố EventTrigger không có một tài sản Binding.
Vậy làm cách nào để thay đổi màu của TextBlock dựa trên Thuộc tính DataContext?
XAML:
<Window x:Class="TestTrigger123345.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<StackPanel HorizontalAlignment="Left">
<TextBlock Text="{Binding Status}">
<TextBlock.Triggers>
<DataTrigger Binding="{Binding Status}" Value="off">
<Setter Property="TextBlock.Background" Value="Red"/>
</DataTrigger>
</TextBlock.Triggers>
</TextBlock>
</StackPanel>
</Window>
Code:
namespace TestTriggers
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
DataContext = this;
Status = "off";
}
public string Status { get; set; }
}
}