2010-08-13 36 views
1

我有一個ComboBox需要依賴另一個ComboBox的值。這部分已經工作,與相關ComboBox刷新時,如果選擇在獨立ComboBox一個新值:WPF,綁定到依賴組合框的屬性總是給出初始值

<!-- Independent --> 
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" 
      x:Name="cbo_product" VerticalAlignment="Center" Width="120" 
      ItemsSource="{Binding Source={StaticResource productsXml}}" 
      DisplayMemberPath="@name" SelectedValuePath="@name" 
      SelectionChanged="cbo_product_SelectionChanged" 
      SelectedValue="{Binding Path=Product}" /> 

<!-- Dependent --> 
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2" 
      x:Name="cbo_component" VerticalAlignment="Center" Width="201" 
      DataContext="{Binding SelectedItem, ElementName=cbo_product}" 
      ItemsSource="{Binding XPath=Components/Component}" 
      DisplayMemberPath="@name" SelectedValuePath="@name" 
      SelectedValue="{Binding Path=Component}" 
      SelectionChanged="cbo_component_SelectionChanged" /> 

在這背後的C#類,我有:

public MyUserControlConstructor() 
{ 
    MyViewModelInstance= new MyViewModel(); 
    DataContext = MyViewModelInstance; 
} 

而在MyViewModel,我有:

public string Component 
{ 
    get { return _component; } 
    set 
    { 
     if (value == _component) 
     { 
      return; 
     } 
     _component = value; 
     onPropertyChanged(PropertyNames.Component); 
    } 
} 

private void onPropertyChanged(PropertyNames fieldName) 
{ 
    if (null == PropertyChanged) 
    { 
     return; 
    } 
    string propertyName = Enum.GetName(typeof(PropertyNames), fieldName); 
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
} 

當我改變依賴ComboBox(組件),它顯示了新的VAL當然,我在我的應用程序中。但是,當我點擊一個導致顯示Component屬性值的按鈕時,它始終是初始值,而不是我剛纔在ComboBox中選擇的值。我認爲我的XAML中一定有錯誤。對於C#,我試圖遵循thisthis guide的組合。如何將我的依賴項ComboBox綁定到嵌套在獨立ComboBoxSelectedItem中的XML值,但仍會更新我的類中的Component屬性?

編輯:我懷疑的是,事情是靠不住的,因爲我設置DataContext的依賴ComboBox在兩個地方:在XAML首先在C#構造函數中,我的視圖模型,二是DataContext="{Binding SelectedItem, ElementName=cbo_product}"

編輯:我一直在構造函數中設置初始值到我的視圖模型類。當我取出Component屬性的初始值時,即使在更改了從屬ComboBox中的選定值之後,我仍然從Component屬性中獲得任何值。這幾乎只是改變了我已經知道的內容:依賴的ComboBox與獨立的ComboBox(它從獨立的ComboBox獲取其數據)綁定,但不是Component屬性。

編輯:的要求,這裏是我的XML的一個樣本:

<Products xmlns=""> 
    <Product name="Awesomeness"> 
    <Components> 
     <Component name="Component of Glory"/> 
     <Component name="Component of Doom"/> 
    </Components> 
    </Product> 
</Products> 

編輯:我猜一個MultiBinding會使用,看着thisthis後。

編輯:好像我應該能夠得到相關ComboBox無需設置DataContext工作,只是利用ItemsSource

<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2" 
         x:Name="cbo_component" VerticalAlignment="Center" Width="201" 
         ItemsSource="{Binding ElementName=cbo_product, Path=SelectedItem, 
          XPath=Components/Component}" 
         DisplayMemberPath="@name" SelectedValuePath="@name" 
         SelectedValue="{Binding Path=Component}" 
         SelectionChanged="cbo_component_SelectionChanged"/> 

然而,這不起作用:因ComboBox是空的,而不是顯示所有的組件名稱。

+1

如果您概述了數據(XML?),會稍微容易一些。 – 2010-08-13 18:48:52

+0

@亨克:正確。用示例更新了我的問題。 – 2010-08-13 18:52:00

回答

0

我發現解決這個問題的方式涉及在C#中設置ItemsSource而不是XAML,我更喜歡而不是。然而,它是有效的,經過一天半的打擊後,這是我想出的最好的。

在XAML:

<!-- Independent --> 
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="2" Grid.Column="2" 
      x:Name="cbo_product" VerticalAlignment="Center" Width="120" 
      ItemsSource="{Binding Source={StaticResource productsXml}}" 
      DisplayMemberPath="@name" SelectedValuePath="@name" 
      SelectionChanged="cbo_product_SelectionChanged" 
      SelectedItem="{Binding Path=ProductNode}" 
      SelectedValue="{Binding Path=Product}" /> 

<!-- Dependent --> 
<ComboBox Height="23" HorizontalAlignment="Left" Grid.Row="3" Grid.Column="2" 
      x:Name="cbo_component" VerticalAlignment="Center" Width="201" 
      DisplayMemberPath="@name" SelectedValuePath="@name" 
      SelectedValue="{Binding Path=Component, Mode=TwoWay}" 
      SelectionChanged="cbo_component_SelectionChanged"/> 

在C#中,事件處理程序時,獨立ComboBox變化:

private void cbo_product_SelectionChanged(object sender, 
    SelectionChangedEventArgs e) 
{ 
    // Set ItemsSource of dependent ComboBox 
    cbo_component.ItemsSource = getChildNodesFromComboBox(
     sender as ComboBox, "Components/Component" 
    ); 
    cbo_component.SelectedIndex = 0; 
} 

// Helper method to do XPath query and get child nodes from selected value of 
// independent ComboBox 
private XmlNodeList getChildNodesFromComboBox(ComboBox comboBox, 
    string xpath) 
{ 
    if (null == comboBox) 
    { 
     return null; 
    } 
    var xml = comboBox.SelectedItem as XmlElement; 
    if (null == xml) 
    { 
     return null; 
    } 
    return xml.SelectNodes(xpath); 
} 

現在Component財產在我看來模型類,到我的依賴ComboBox是綁定在XAML中,因爲我不必更改從屬ComboBoxDataContext,所以在相關的ComboBox中選擇的值被填充。