Silverlight 使用ItemPanel绑定行为奇怪,不使用ItemPanel绑定行为正常
上的以下代码显示Silverlight 使用ItemPanel绑定行为奇怪,不使用ItemPanel绑定行为正常,silverlight,windows-phone-7,dependency-properties,silverlight-toolkit,itemspaneltemplate,Silverlight,Windows Phone 7,Dependency Properties,Silverlight Toolkit,Itemspaneltemplate,上的以下代码显示{Binding text},并且精灵的依赖项属性不运行propertyvaluechanged(属性值已更改,用于文本运行,但不用于精灵) <ItemsControl x:Name="AnswerListBox" ItemsSource="{Binding Answers}" ScrollViewer.VerticalScrollBarVisibility="Disabled" > <ItemsControl.ItemTemplate>
{Binding text}
,并且精灵的依赖项属性不运行propertyvaluechanged(属性值已更改,用于文本运行,但不用于精灵)
<ItemsControl x:Name="AnswerListBox" ItemsSource="{Binding Answers}" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:spriteRadioButton Text="{Binding text}" Sprites="{Binding Path=DataContext.UISprites, ElementName=questionField}" GroupName="{Binding Path=DataContext.QuestionTitle, ElementName=questionField}" IsChecked="{Binding selected}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
如果我不使用itemspaneltemplate,那么属性将按预期工作。目前您正在使用默认的“单向”绑定机制。这意味着您的对象可以更新UI,但UI无法更新对象 您的绑定应使用“双向”绑定,以允许UI通知对象更改:
<DataTemplate>
<local:spriteRadioButton Text="{Binding text,Mode=TwoWay}" Sprites="{Binding Path=DataContext.UISprites, ElementName=questionField,Mode=TwoWay}" GroupName="{Binding Path=DataContext.QuestionTitle, ElementName=questionField,Mode=TwoWay}" IsChecked="{Binding selected,Mode=TwoWay}" />
</DataTemplate>
请记住,这些更改将更新您的Answers对象。如果要更改Answers对象本身,也需要将其标记为双向绑定。文本、精灵和组名不需要更新,只需更新选中的值即可。但是如果我改变了对象,检查就不会发生。我必须刷新datacontext吗?当你说对象时,你是指“应答”对象吗?您的“应答”对象将需要实现INotifyPropertyChanged。这意味着当您将“selected”属性设置为true时,将引发NotifyPropertyChanged事件。