简体   繁体   English

WPF中的ComboBox模板问题

[英]ComboBox template problems in WPF

I have a listview with a DataTemplate that has a ComboBox. 我有一个带有ComboBox的DataTemplate的listview。 I want the ComboBox to look flat like a label until the user actually wants to change the value. 我希望ComboBox看起来像标签一样平直,直到用户真正想要更改值。 I had the example below working before, but I changed things around a bit, and now it doesn't work anymore and I'm not sure why. 我之前有过这样的例子,但我改变了一些东西,现在它不再起作用了,我不知道为什么。

The IsMouseOver property does not seem to be working correctly, as it only gets set when the mouse is right at the border of the control. IsMouseOver属性似乎无法正常工作,因为它只在鼠标位于控件边框时才会设置。

What can I do to make this work correctly? 我该怎么做才能使这项工作正常进行?

Here is a snippet: 这是一个片段:

  <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> 

I took the code you provided, supplied some data for the collections, and it worked just like you wanted it to. 我拿了你提供的代码,为集合提供了一些数据,它就像你想要的那样工作。 I would suggest using Snoop to look to see if there are any other elements consuming the events you expect the ListView to handle. 我建议使用Snoop来查看是否有任何其他元素消耗了您期望ListView处理的事件。

Usually when your are having issues with Mouse events firing correctly it's due to the background missing. 通常当您遇到鼠标事件正确触发问题时,由于背景缺失。 If the element you expect to receive events has a null reference for a background, the control will not receive the events; 如果您希望接收事件的元素具有背景的空引用,则控件将不会接收事件; only the control underneath of it will. 只有它下面的控件才会。 Try setting the background of your ContentControl to "Transparent". 尝试将ContentControl的背景设置为“透明”。 That should fix your problem. 这应该可以解决你的问题。

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

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