2013-08-16 38 views
2

對c#和xaml來說相當新,所以請耐心等待!我有兩個組合框,每個顯示的值都相互依賴。要做到這一點我用的DisplayMemberPath和SelectedMemberPath像這樣:兩個組合框中空的第一個值互相更新

<ComboBox SelectedIndex="-1" x:Name="SiteLocCombo" SelectedValuePath="GeneralArea" DisplayMemberPath="Station" ItemSource="{Binding}"/> 
<Combobox SelectedIndex="-1" x:Name="SiteCodeCombo" SelectedValuePath="Station" DisplayMemberPath="GeneralArea" ItemSource="{Binding}"/> 

若要獲取組合框的第一個值我添加屬性IsSynchronizedWithCurrentItem=True 但是現在的組合框不更新對方

回答

1

如果你想在第二組合跟蹤第一個組合的選定值...

<ComboBox SelectedIndex="-1" 
        x:Name="SiteLocCombo" 
        SelectedValuePath="GeneralArea" 
        DisplayMemberPath="Station" 
        ItemSource="{Binding}"/> 
     <ComboBox SelectedIndex="-1" 
        x:Name="SiteCodeCombo" 
        SelectedValuePath="Station" 
        DisplayMemberPath="GeneralArea" 
        ItemSource="{Binding}" 
        SelectedValue="{Binding ElementName=SiteLocCombo, Path=SelectedValue}" 
        /> 

或者,您可以綁定到SelectedIndex的,或selectedItem等以同樣的方式。這適用於兩者都綁定到相同的ItemsSource的情況,否則在ViewModel中需要一個轉換器或一個屬性。

+0

@FionaByrne,很高興有幫助。你是新的,所以請閱讀此通報... http://meta.stackoverflow.com/help/someone-answers –

+0

謝謝!最後有用的代碼是SelectedValue,但我也從兩者中刪除了'SelectedValuePath',並設置了'IsSynchronizedWithCurrentItem ='False''這個組合框互相更新,但不顯示啓動時的第一個值形成。 – CatOfTheCanals

相關問題