简体   繁体   English

奇怪的组合框行为-WPF

[英]Strange ComboBox Behaviour - WPF

I've got the following user-control: 我有以下用户控件:

Resources: 资源:

    <DataTemplate x:Key="FilterComboDataTemplate">
        <Label Content="{Binding Item2}" />
    </DataTemplate>

    <Style x:Key="FilterSelectorStyle" TargetType="ComboBox">
        <Setter Property="ItemsSource" Value="{Binding Filters}" />
        <Setter Property="SelectedItem" Value="{Binding SelectedFilter}" />
        <Setter Property="ItemTemplate" Value="{StaticResource FilterComboDataTemplate}" />
    </Style>

Control Body: 控制体:

<DockPanel>
    <Label DockPanel.Dock="Top">
        Select your filter/value to apply:
    </Label>
    <ComboBox Style="{StaticResource FilterSelectorStyle}" />
    <StackPanel>
        <!-- TODO: Fix Combobox First -->
    </StackPanel>
</DockPanel>

It's inside a <Window> and opened using .ShowDialog() , this is what happens to the items when I click on the button: 它位于<Window>并使用.ShowDialog()打开,这是当我单击按钮时项目发生的情况:

Undesirable Results http://img827.imageshack.us/img827/1561/whyowhy.png 不良结果http://img827.imageshack.us/img827/1561/whyowhy.png

I'm at a complete loss as to why this is happening, I've checked the visual tree, everything's where it should be. 我完全不知道为什么会发生这种情况,我检查了可视化树,所有内容都在应有的位置。 I'm baffled. 我很困惑。 Anyone out there experienced strange behavior like this? 外面有人遇到过这种奇怪的行为吗? Why are my items at 0,0 on my desktop instead of attached to my combobox? 为什么我的项目在桌面上的位置是0,0,而不是附加到组合框?

I quickly coded this. 我很快对此进行了编码。 Didn't have any problem. 没问题。

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:WpfApplication5="clr-namespace:WpfApplication5" x:Class="WpfApplication5.MainWindow"
        x:Name="MyWindow"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>          
        <DataTemplate x:Key="FilterComboDataTemplate">
            <Label Content="{Binding Item2}" />
        </DataTemplate>
    </Window.Resources> 
    <DockPanel>

        <Label DockPanel.Dock="Top">
            Select your filter/value to apply:
        </Label>

        <ComboBox Height="32" ItemsSource="{Binding Filters, ElementName=MyWindow}" ItemTemplate="{DynamicResource FilterComboDataTemplate}"/>

        <StackPanel>
            <!-- TODO: Fix Combobox First -->
        </StackPanel>

    </DockPanel>
</Window>

Also, I've never seen any one putting ItemSource and SelectedItem in a style. 另外,我从未见过有人将ItemSourceSelectedItem放在一种样式中。 I don't think that's a good WPF practice. 我认为这不是WPF的良好做法。 I will rather bind to a ICollectionView which allows grouping, filtering, managing cursor etc 我宁愿绑定到允许分组,过滤,管理游标等的ICollectionView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM