DataTrigger未触发

时间:2014-05-19 20:37:17

标签: c# wpf data-binding inotifypropertychanged datatrigger

我在这里感到很沮丧......我无法让这些数据触发器始终如一地工作......

当我第一次运行程序时,我初始化一个全局UdpMessageAuthentication类(因为它将它设置为" test0" ...但是我有一个调用SendAuthPacket方法的按钮...来自调试当我点击按钮时看到它进入OnPropertyChanged,但是标签不会改变标题或颜色或任何其他属性......一旦我使用AuthenticateStatus来测试" test1"。

显然我尝试了除test0和test1之外的更实际的变量,但无论我做什么我都无法获得更新的触发器

请帮助= 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....
    }

1 个答案:

答案 0 :(得分:1)

确保在使用(任意)绑定时明确设置Mode Property

<DataTrigger Binding="{Binding Path=AuthenticateStatus, Mode=OneWay}" Value="test0">

此外,您必须确保在将PropertyName字符串设置为正在引发的proprty的名称时引发属性更改事件,因为系统正在使用Reflection来根据所述查找已更改的属性串。因此,请尝试在ViewModel中使用它:

get { return _authenticateStatus; }
set
{
    if (_authenticateStatus != value)
    {
        _authenticateStatus = value;
        OnPropertyChanged("AuthenticateStatus");
    }
}