gpt4 book ai didi

c# - NotifyPropertyChanged 未触发

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:28 28 4
gpt4 key购买 nike

我正在尝试为 ListBox 捕获 SelectedItem,然后为 Button 触发数据模板单击事件。我在 notifypropertychanged 事件处理程序中放置了一个断点,但它从未触发。我在这里做错了什么?

xaml:

   <ListBox x:Name="lstbox_playerContainer"
ItemsSource="{Binding ChildObjectOC}"
SelectedItem="{Binding SelectedChildObject, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Name="btn_childButton" Click="btn_childButton_Click"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

c#:

public partial class PlayerCrowdPromptPage: INotifyPropertyChanged {

public PlayerCrowdPromptPage() {
InitializeComponent();
DataContext = this;
}

private ObservableCollection<PlayerCrowdObjectBO> childObjectOC = new ObservableCollection<PlayerCrowdObjectBO>();
public ObservableCollection<PlayerCrowdObjectBO> ChildObjectOC {
get {
return childObjectOC;
}
set {
childObjectOC = value;
}
}

private PlayerCrowdObjectBO selectedChildObject;
public PlayerCrowdObjectBO SelectedChildObject {
get { return selectedChildObject; }
set {
selectedChildObject = value;
OnPropertyChanged("SelectedChildObject");
}
}

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName) {
if (PropertyChanged != null){
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

最佳答案

点击 Button 不会自动选择相应的项目。您可以在事件处理程序中以编程方式选择它:

private void btn_childButton_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
ListBoxItem lbi = lstbox_playerContainer.ItemContainerGenerator.ContainerFromItem(btn.DataContext) as ListBoxItem;
lbi.IsSelected = true;
}

这应该设置 SelectedChildObject 属性并引发 PropertyChanged 事件。

关于c# - NotifyPropertyChanged 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49654214/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com