【问题标题】:Binding acting strange with ItemPanel and ok without绑定与 ItemPanel 的行为很奇怪,没有
【发布时间】:2011-10-23 22:23:09
【问题描述】:

下面的代码显示 {Binding text} 和 Sprites 的依赖属性不运行 propertyvaluechanged 用于文本运行但不用于 sprites。

<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,那么属性会按预期工作。

【问题讨论】:

    标签: silverlight windows-phone-7 dependency-properties silverlight-toolkit itemspaneltemplate


    【解决方案1】:

    目前您正在使用默认的“OneWay”绑定机制。这意味着您的对象可以更新 UI,但 UI 不能更新对象。

    您的绑定应使用“TwoWay”绑定以允许 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 对象本身,也需要将其标记为 TwoWay 绑定。

    【讨论】:

    • 文本、精灵和组名不需要只更新选中的值。但是如果我更改对象,则不会进行检查。我必须刷新数据上下文吗?
    • 当您说对象时,您的意思是“答案”对象吗?您的“Answer”对象将需要实现 INotifyPropertyChanged。这意味着当您将“selected”属性设置为 true 时,将引发 NotifyPropertyChanged 事件。
    猜你喜欢
    • 1970-01-01
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    相关资源
    最近更新 更多