2013-02-15 85 views
0

我有一個組合框,如下所示。爲什麼代碼隱藏不是始終開啓?WPF組合框不選擇觸發

XAML:

<ComboBox Height="23" 
         Name="cbAppendCreate" 
         VerticalAlignment="Top" 
         Width="120" 
         Margin="{StaticResource ConsistentMargins}" 
         ItemsSource="{Binding Path=CbCreateAppendItems}" 
         SelectedValue="{Binding Path=CbAppendCreate,UpdateSourceTrigger=PropertyChanged}" /> 

代碼隱藏:

private string cbAppendCreate; 
public string CbAppendCreate { 
    get { 
     //.... 
     return cbAppendCreate 
    } 
    set { //This doesn't fire when selecting the first of 2 Items, 
      //but always fires when selecting the 2nd of two items 
      //.... 
     cbAppendCreate = value; 
    } 
} 
+0

輸出窗口中的任何綁定錯誤? – Haspemulator 2013-02-15 21:07:00

+0

@Haspemulator號碼 – sammarcow 2013-02-15 21:16:51

+0

無法在我簡單的設置中重現。發佈更多的代碼,或者我可以發佈我的,如果你想。 – Haspemulator 2013-02-15 21:41:38

回答

0

我會在這裏發佈我的工作代碼,這是非常簡單的。我剛剛使用VS2012模板創建了一個默認的WPF應用程序。這裏的MainWindow.xaml內容:

<Window x:Class=" 
    WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<StackPanel> 
    <ComboBox Height="23" 
        Name="cbAppendCreate" 
        VerticalAlignment="Top" 
        Width="120" 
        ItemsSource="{Binding Path=CbCreateAppendItems}" 
        SelectedValue="{Binding Path=CbAppendCreate,UpdateSourceTrigger=PropertyChanged}" /> 
    <TextBlock Text="{Binding CbAppendCreate}"></TextBlock> 
</StackPanel> 

這裏的隱藏代碼:

namespace WpfApplication1 
    { 
    public class DataSource 
    { 
     public List<string> CbCreateAppendItems { get; set; } 
     public string CbAppendCreate { get; set; } 
     public DataSource() 
     { 
      CbCreateAppendItems = new List<string>() { "create", "append" }; 
     } 
    } 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
      DataContext = new DataSource(); 
     } 
    } 
} 

當我選擇在組合框中不同的值,將TextBlock更新爲相同的值,因此,虛擬機的財產也被更新。

+0

我接受這個,因爲它的工作原理,我創建了一個可以在每個選擇上觸發的工作示例,但是我的問題仍然與原始問題一致。 setter不會觸發ComboBox值發生任何變化。我不確定錯誤在哪裏。 – sammarcow 2013-02-19 19:32:58