我在这里很沮丧。我不能让这些数据触发器始终工作…
当我第一次运行程序时,我初始化一个全局UdpMessageAuthentication类(因为它将其设置为"test0"…然后我有一个按钮调用SendAuthPacket方法。从调试我看到它进入OnPropertyChanged当我点击按钮,但标签不会改变标题或颜色或任何其他属性…一旦我使用AuthenticateStatus "test1"
显然,除了test0和test1之外,我尝试了更现实的变量,但无论我做什么,我都无法获得更新
的触发器Please help =T
<Label Name="Label_Authentication" Margin="5,0,0,0" VerticalAlignment="Center" Grid.Column="0" FontSize="14">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Content" Value="Initial Content"></Setter>
<Setter Property="Foreground" Value ="Red"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=AuthenticateStatus}" Value="test0">
<Setter Property="Content" Value="Authentication Required" />
<Setter Property="Foreground" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=AuthenticateStatus}" Value="test1">
<Setter Property="Content" Value="Attempting Authentication..." />
<Setter Property="Background" Value="Blue"></Setter>
</DataTrigger>
etc....
public class UdpMessageAuthentication : INotifyPropertyChanged
{
private string _authenticateStatus;
public string AuthenticateStatus
{
get { return _authenticateStatus; }
set
{
if (_authenticateStatus != value)
{
_authenticateStatus = value;
OnPropertyChanged("Authenticate Status");
}
}
}
public UdpMessageAuthentication()
{
_udpClient = new UdpClient();
AuthenticateStatus = "test0";
}
public void SendAuthPacket(IPAddress ip, string userID)
{
etc etc....
AuthenticateStatus = "test1";
etc etc....
}
确保在使用(任何)绑定时显式设置Mode属性。
<DataTrigger Binding="{Binding Path=AuthenticateStatus, Mode=OneWay}" Value="test0">
另外,您必须确保您正在引发属性更改事件,并且PropertyName字符串设置为正在引发的属性的名称,因为系统正在使用底层反射来根据所述字符串查找已更改的属性。因此,请尝试在ViewModel中使用:
get { return _authenticateStatus; }
set
{
if (_authenticateStatus != value)
{
_authenticateStatus = value;
OnPropertyChanged("AuthenticateStatus");
}
}