WPF中的ComboBox模板问题

时间:2008-12-03 17:31:37

标签: c# wpf listview combobox datatemplate

我有一个带有ComboBox的DataTemplate的列表视图。我希望ComboBox看起来像标签一样平直,直到用户真正想要更改值。我之前有过这样的例子,但我改变了一些东西,现在它不再起作用了,我不知道为什么。

IsMouseOver属性似乎无法正常工作,因为它只在鼠标位于控件边框时才会设置。

如何才能使其正常工作?

这是一个片段:

    <CollectionViewSource x:Key="AccountCategories" />

    <ControlTemplate x:Key="FlatCombo" TargetType="{x:Type ComboBox}">
        <ContentControl
            Content="{TemplateBinding SelectionBoxItem}"
            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"

            Margin="4,3,3,3" 
            />
    </ControlTemplate>

    <Style TargetType="{x:Type ComboBox}" x:Key="DropDown">
        <Setter Property="OverridesDefaultStyle" Value="False" />            
        <Style.Triggers> 
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsMouseOver" Value="False" />                       
                    <Condition Property="IsFocused" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Template" Value="{StaticResource FlatCombo}" />
            </MultiTrigger>
        </Style.Triggers>
    </Style>

    <DataTemplate x:Key="Category">
        <ComboBox IsSynchronizedWithCurrentItem="False" Style="{StaticResource DropDown}"
                                      ItemsSource="{Binding Source={StaticResource db}, Path=Categories}" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding Path=Category}" />
    </DataTemplate>
</Window.Resources>

<Grid>


    <ListView Margin="0,110,0,0" Name="lstCategories" ItemsSource="{Binding Source={StaticResource AccountCategories}}" Grid.RowSpan="2">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Category" Width="100"  CellTemplate="{StaticResource Category}" />
                <GridViewColumn DisplayMemberBinding="{Binding Path=Balance}" Header="Balance" Width="100" />
            </GridView>
        </ListView.View>
    </ListView>

<CollectionViewSource x:Key="AccountCategories" /> <ControlTemplate x:Key="FlatCombo" TargetType="{x:Type ComboBox}"> <ContentControl Content="{TemplateBinding SelectionBoxItem}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Margin="4,3,3,3" /> </ControlTemplate> <Style TargetType="{x:Type ComboBox}" x:Key="DropDown"> <Setter Property="OverridesDefaultStyle" Value="False" /> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsMouseOver" Value="False" /> <Condition Property="IsFocused" Value="False"/> </MultiTrigger.Conditions> <Setter Property="Template" Value="{StaticResource FlatCombo}" /> </MultiTrigger> </Style.Triggers> </Style> <DataTemplate x:Key="Category"> <ComboBox IsSynchronizedWithCurrentItem="False" Style="{StaticResource DropDown}" ItemsSource="{Binding Source={StaticResource db}, Path=Categories}" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding Path=Category}" /> </DataTemplate> </Window.Resources> <Grid> <ListView Margin="0,110,0,0" Name="lstCategories" ItemsSource="{Binding Source={StaticResource AccountCategories}}" Grid.RowSpan="2"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn Header="Category" Width="100" CellTemplate="{StaticResource Category}" /> <GridViewColumn DisplayMemberBinding="{Binding Path=Balance}" Header="Balance" Width="100" /> </GridView> </ListView.View> </ListView>

2 个答案:

答案 0 :(得分:2)

我拿了你提供的代码,为集合提供了一些数据,它就像你想要的那样工作。我建议使用Snoop来查看是否有任何其他元素消耗了您期望ListView处理的事件。

答案 1 :(得分:0)

通常当您遇到鼠标事件正确触发问题时,由于背景缺失。如果您希望接收事件的元素具有背景的空引用,则控件将不会接收事件;只有它下面的控件才会。尝试将ContentControl的背景设置为“透明”。这应该可以解决你的问题。