2013-02-15 83 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