2013-07-09 35 views
1

我有一個組合框不工作,因爲我期望在運行時。我可以使用鼠標來展開下拉窗口,但單擊一個項目似乎並未選擇它。下拉菜單消失,但選擇不會改變。使用鍵盤,相同的控件似乎能夠按預期工作。向上/向下箭頭更改選擇。我可以使用箭頭鍵選擇並進入以選擇更改值。組合框項目選擇與鍵盤,但不是鼠標

如何獲得點擊以選擇項目?

<DataTemplate DataType="{x:Type myType}"> 
    <Border ...> 
     <Grid x:Name="upperLayout"> 
     <Grid x:Name="lowerLayout"> 
      <ComboBox x:Name="combo" 
      Grid.Column="2" 
      ItemsSource="{Binding Things}" 
      SelectedItem="{Binding SelectedThing}" 
      > 
      <ComboBox.ItemTemplate> 
       <DataTemplate> 
       <TextBlock Text="{Binding Name}" /> 
       </DataTemplate> 
      </ComboBox.ItemTemplate> 
      </ComboBox> 
     </Grid> 
     </Grid> 
    </Border> 
    </DataTemplate> 
+1

雖然當您更改SelectedItem時,SelectedThing會改變嗎? – PoweredByOrange

+0

可能相關http://stackoverflow.com/questions/1342200/wpf-combobox-not-responding-to-mouse – keyboardP

+0

您是否嘗試查看SelectionChanged是否被調用?只需添加事件作爲方法和斷點。 – Master117

回答

0

根本原因是另一個開發人員實現了一些代碼,這些代碼改變了預覽鼠標向下事件的焦點。此代碼已更新爲具有所需的行爲而無需修改焦點,組合框現在按預期工作。診斷所需的信息不在原始問題中(無法公佈所有...)。

1

我真的不能告訴什麼是從你的代碼錯不過,我強烈建議你使用史努比調試你的控件(http://snoopwpf.codeplex.com/

通過按住Ctrl + Shift的鼠標指向哪裏你ComboBox是應該抓住輸入,你會立即發現誰有焦點,而不是你的組合框。

enter image description here

你甚至可以改變一個屬性的值,真的是你最好的朋友調試模板!

編輯

我害怕,但你的代碼已經發布的作品對我來說:

<Window x:Class="WpfApplication6.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:wpfApplication6="clr-namespace:WpfApplication6" 
     Title="MainWindow" 
     Width="525" 
     Height="350"> 
    <Window.Resources> 
     <DataTemplate x:Key="myTemplate" DataType="{x:Type wpfApplication6:MyType}"> 
      <Border> 
       <Grid x:Name="upperLayout"> 
        <Grid x:Name="lowerLayout"> 
         <ComboBox x:Name="combo" 
            Grid.Column="0" 
            ItemsSource="{Binding Path=Things}" 
            SelectedItem="{Binding Path=SelectedThing}"> 
          <ComboBox.ItemTemplate> 
           <DataTemplate DataType="{x:Type wpfApplication6:MyThing}"> 
            <TextBlock Text="{Binding Name}" /> 
           </DataTemplate> 
          </ComboBox.ItemTemplate> 
         </ComboBox> 
        </Grid> 
       </Grid> 
      </Border> 
     </DataTemplate> 
    </Window.Resources> 
    <Grid x:Name="grid"> 
     <ContentControl x:Name="content" ContentTemplate="{StaticResource myTemplate}" Margin="58,79,71,40" /> 
    </Grid> 
</Window> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     Loaded += MainWindow_Loaded; 
    } 

    private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
    { 
     MyType type = new MyType() 
      { 
       Things = new List<MyThing>() {new MyThing() {Name = "aaa"}, new MyThing() {Name = "bbb"}} 
      }; 
     content.Content = type; 
    } 
} 

public class MyType 
{ 
    public MyThing SelectedThing { get; set; } 
    public List<MyThing> Things { get; set; } 
} 

public class MyThing 
{ 
    public string Name { get; set; } 
} 

也許別的東西將它擰緊,如無鑰匙風格或無論如何,發佈更多您遇到問題的代碼。

+0

Snoop顯示相應的ComboItem爲焦點。 – PatrickV

+0

我已經更新了我的答案。 – Aybe

相關問題