ListBox绑定在测试配置中不起作用

时间:2017-04-17 17:00:15

标签: c# wpf binding listbox

我在绑定ListBox时遇到了麻烦,我不能为我的生活弄清楚什么是错的。大部分时间它工作正常,但在我的应用程序的特殊测试配置它不起作用,它让我疯了。我已经盯着它看了几个小时,并尝试了各种各样的事情。它必须是一个具有约束力的问题,但我找不到它。我没有看到任何异常或绑定错误。

测试配置与我的调试配置相同,但附加了一个编译符号。它允许我在不运行整个软件堆栈的情况下测试WPF应用程序。所有意图和目的都是“离线模式”。

? go.
|: foo
[foo]
bar
[bar]
true.

这是我的登录方法,其中创建窗口并绑定DataContext。 UITEST是我的离线模式配置。

<ribbon:Ribbon Height="45">
<ribbon:Ribbon.ApplicationMenu>
    <ribbon:RibbonApplicationMenu Width="56">
        <ribbon:RibbonApplicationMenuItem Header="New Campaign" Command="{Binding Path=AddCampaign}" ImageSource="images\new.png"/>
        <ribbon:RibbonApplicationMenuItem Header="Save Campaigns" Command="{Binding Path=Save}" ImageSource="images\save.png" />
        <ribbon:RibbonApplicationMenuItem Header="Import Campaign" Command="{Binding Path=ImportCampaign}" ImageSource="images\campaign-import-128x128.png"/>
        <ribbon:RibbonApplicationMenuItem Header="Export Campaign" Command="{Binding Path=ExportCampaign}" ImageSource="images\campaign-export-128x128.png" />
        <ribbon:RibbonApplicationMenuItem Header="Terms of Use" Command="{Binding Path=Eula}" ImageSource="images\terms.png"/>
        <ribbon:RibbonApplicationMenuItem Header="Resources" Command="{Binding Path=Resources}" ImageSource="images\resource.jpg"/>
        <ribbon:RibbonApplicationMenuItem Header="About" Command="{Binding Path=About}" ImageSource="images\resource.jpg"/>
        <ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <Label Width="130" Content="Campaign"/>
                    <Label Width="60" Content="Duplicate"/>
                    <Label Width="50" Content="Delete"/>
                </StackPanel>
                <ListBox ItemsSource="{Binding Path=Repository.Campaigns}" 
                            SelectedItem="{Binding Path=SelectedCampaign, Mode=TwoWay}" 
                            ItemContainerStyle="{StaticResource ListBoxItemStyle}" 
                            Height="212" 
                            ScrollViewer.VerticalScrollBarVisibility="Visible" 
                            ScrollViewer.HorizontalScrollBarVisibility="Hidden">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Label Content="{Binding Path=Name}" Width="130" />
                                <Button Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.DuplicateCampaign}"
                                    CommandParameter="{Binding Path=Name}" Width="60">
                                    <Button.Template>
                                        <ControlTemplate>
                                            <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                                                <Image Source="/asdf;component/images/duplicate.png" Width="24" Height="24"/>
                                            </Border>
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                                <Button Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemsControl}}, Path=DataContext.RemoveCampaign}"
                                    CommandParameter="{Binding Path=Name}" Width="50">
                                    <Button.Template>
                                        <ControlTemplate>
                                            <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                                                <Image Source="/asdf;component/images/delete.png" Width="24" Height="24"/>
                                            </Border>
                                        </ControlTemplate>
                                    </Button.Template>
                                </Button>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Button Content="View Archived" 
                        Command="{Binding Path=ArchivedCampaigns}"></Button>
            </StackPanel>
        </ribbon:RibbonApplicationMenu.AuxiliaryPaneContent>
        <ribbon:RibbonApplicationMenu.FooterPaneContent>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <ribbon:RibbonButton Label="LOGOUT" Command="{Binding Path=Logout}" />
                <ribbon:RibbonButton Label="EXIT" Command="{Binding Path=Exit}" />
            </StackPanel>
        </ribbon:RibbonApplicationMenu.FooterPaneContent>
    </ribbon:RibbonApplicationMenu>
</ribbon:Ribbon.ApplicationMenu>

编辑:尽可能接近我的猜测,可能是因为使用了.NET 4.5之前的旧版RibbonControlsLibrary。我去年将该软件重新定位到.NET 4.6.1,所以我切换到.NET内置的功能区,现在它的工作原理很奇怪。如果我试图弄乱窗口中的任何其他东西,它似乎再次破裂。就像我尝试在窗口中强烈输入DataContext一样。

1 个答案:

答案 0 :(得分:0)

尽可能地猜测,这可能是因为使用了.NET 4.5之前的旧版RibbonControlsLibrary。我去年将该软件重新定位到.NET 4.6.1,所以我切换到.NET内置的功能区,现在它的工作原理很奇怪。如果我试图弄乱窗口中的任何其他东西,它似乎再次破裂。就像我尝试在窗口中强烈输入DataContext一样。