开发者

WPF: Binding Customized ListBox & List<T>: PropertyChanged is always null

i have customized ListBox declared in XAML:

<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>

I need to dsiplay and interop with generic List, where T is:

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 is pretty simple Base class:

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

When i turn Activated property programmatically, checkbox do not change state. Debug: PropertyChan开发者_高级运维ged = null. Anybody know, what is incorrect?


One obvious problem meets the eye: TemplatedParent is for use with ControlTemplates. Since you're using a DataTemplate, this should work:

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

I didn't notice any problems with the C#.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜