WPF:绑定自定义ListBox&amp; List <t>:PropertyChanged始终为null </t>

时间:2010-01-21 08:16:24

标签: wpf data-binding inotifypropertychanged

我已自定义在XAML中声明的ListBox:

<ListBox x:Name="uicMDSQonfServer">
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel Orientation="Horizontal"
                  Margin="0,5,0,5">
        <CheckBox IsChecked="{Binding RelativeSource={TemplatedParent},
                                      Path=Activated}" />
        <ContentPresenter Content="{Binding RelativeSource={TemplatedParent},
                                    Path=Content}"/>
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

我需要dsiplay和interop与通用List,其中T是:

public class QonfServer: QonfBase, INotifyPropertyChanged
{
        private string ip;
        private bool activated;

        public string Ip {
            get { return ip; }
        }

        public bool Activated
        {
            get { return activated; }
            set
            {
                if (activated == value)
                    return;

                activated = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Activated"));
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }

QonfBase非常简单基类:

public class QonfBase
{
        private int id;
        public  int ID { get; set; }
}

当我以编程方式打开Activated属性时,复选框不会更改状态。 Debug:PropertyChanged = null。有人知道,有什么不对吗?

1 个答案:

答案 0 :(得分:1)

一个明显的问题出现了:TemplatedParent用于ControlTemplate。由于您使用的是DataTemplate,因此应该可以使用:

<CheckBox IsChecked="{Binding Activated}" /> 
<ContentPresenter Content="{Binding Content}"/> 

我没有发现C#有任何问题。