WP7:为什么 ListBox.ItemsPanel 会破坏我的 ElementName 数据绑定? [英] WP7: Why does a ListBox.ItemsPanel break my ElementName data binding?

查看:19
本文介绍了WP7:为什么 ListBox.ItemsPanel 会破坏我的 ElementName 数据绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到整数列表的 Windows Phone 7 ListBox.我使用的是默认的 MVVM Light 模板,因此有一个 ViewModel 类,其中包含数据和一个简单的 RelayCommand.这是列表框:

<ListBox.ItemTemplate><数据模板><按钮内容="{绑定}"><i:Interaction.Triggers><i:EventTrigger EventName="点击"><cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}"CommandParameter="{Binding}"/></i:EventTrigger></i:Interaction.Triggers></按钮></数据模板></ListBox.ItemTemplate></列表框>

这会显示按钮内整数的垂直列表.如果单击其中任何一个,以下命令代码将执行并显示一个弹出窗口:new RelayCommand(i => MessageBox.Show("Test" + i));>

但是,如果我只是添加以下 XAML 以更改为水平列表,则数据绑定将失败.当您单击按钮时没有任何反应,并且没有错误消息写入输出窗口.

<ItemsPanelTemplate><StackPanel Orientation="Horizo​​ntal" Horizo​​ntalAlignment="Center"/></ItemsPanelTemplate></ListBox.ItemsPanel>

我为 EventToCommand 尝试了一些其他类型的绑定.例如,将我的 ViewModel 指定为静态资源.它有效,但不如上面的示例理想.

为什么 ItemsPanel 会破坏数据绑定?

解决方案

这是 Silverlight 3 的一个已知问题.要解决此问题,请将您的 DataTemplate 包装在 UserControl 中:

 <按钮内容="{绑定}"><i:Interaction.Triggers><i:EventTrigger EventName="点击"><cmd:EventToCommand Command="{Binding ElementName=ContentGrid, Path=DataContext.TestCommand}"CommandParameter="{Binding}"/></i:EventTrigger></i:Interaction.Triggers></按钮></用户控件>

改用它:

<ListBox.ItemTemplate><数据模板><local:MyUserControl/></数据模板></ListBox.ItemTemplate></列表框>

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 contains data and a simple RelayCommand. Here is the ListBox:

<ListBox ItemsSource="{Binding MyData}">
    <ListBox.ItemTemplate>
        <DataTemplate>                        
            <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>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

This displays a vertical list of integers inside buttons. If you click any of them, the following command code executes and shows a pop-up: new RelayCommand<int>(i => MessageBox.Show("Test" + i));

However, if I simply add the following XAML to change to a horizontal list, the databinding fails. Nothing happens when you click the button and no error messages are written to the Output window.

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

I have tried some other types of binding for the EventToCommand. For example, specifying my ViewModel as a static resource. It works, but is less ideal than the example above.

Why does that ItemsPanel break the databinding?

解决方案

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>

这篇关于WP7:为什么 ListBox.ItemsPanel 会破坏我的 ElementName 数据绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆