WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?

前端 未结 1 1021
暖寄归人
暖寄归人 2020-12-19 21:29

I have a Windows Phone 7 ListBox that binds to a list of integers. I am using the default MVVM Light template, so there is a ViewModel class that

相关标签:
1条回答
  • 2020-12-19 21:49

    This is a known problem with Silverlight 3. To work around this, wrap your DataTemplate in a UserControl:

        <UserControl x:Class="SilverlightApplication.MyUserControl">
            <Button Content="{Binding}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="Click">
                        <cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}"
                                            CommandParameter="{Binding}" />
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </Button>
        </UserControl>
    

    And use it instead:

    <ListBox ItemsSource="{Binding MyData}">
        <ListBox.ItemTemplate>
            <DataTemplate>                        
                <local:MyUserControl />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    
    0 讨论(0)
提交回复
热议问题