2011-04-18 55 views
4

我有這個問題,必須綁定列表視圖中嵌入的組合框的選定值。我在顯示組合框中的項目時沒有問題。但是,我希望我能夠通過單擊按鈕來指定組合框應該顯示的內容(從它保存的項目中)。我認爲在這個問題上有幾個帖子,但是,我無法得到我想要的。這是我的代碼。在列表視圖中綁定到WPF組合框(2路)

XAML:

<Grid> 
    <StackPanel Orientation="Vertical"> 
    <ListView 
     x:Name="OptionsListView" 
     ItemsSource="{Binding MyObjectCollection}"> 
     <ListView.Resources> 
      <DataTemplate x:Key="comboBoxTemplate"> 
       <ComboBox 
         Margin="0,3" 
         x:Name="MyTypeComboBox" 
         SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}"> 
        <ComboBoxItem Content="ABC"/> 
        <ComboBoxItem Content="DEF"/> 
        <ComboBoxItem Content="XYZ"/> 
       </ComboBox> 
      </DataTemplate> 
     </ListView.Resources> 
     <ListView.View> 

      <GridView> 
       <GridViewColumn Header="Text-Sample" 
            Width="100"> 
        <GridViewColumn.CellTemplate> 
         <DataTemplate> 
          <TextBlock Text="{Binding Name}"/> 
         </DataTemplate> 
        </GridViewColumn.CellTemplate> 
       </GridViewColumn> 
       <GridViewColumn Header="Combo-Sample" 
            Width="100" 
            CellTemplate="{StaticResource comboBoxTemplate}" /> 
      </GridView> 
     </ListView.View> 
    </ListView> 
    <Button Click="Button_Click">Click Me!</Button> 
    </StackPanel> 
</Grid> 

C#代碼背後:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     OptionsListView.DataContext = this; 
    } 

    private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     //Something here that dictates what should be displayed in the combo box 
    } 

    List<MyObject> myObjectCollection = new List<MyObject>(); 
    public List<MyObject> MyObjectCollection 
    { 
     get 
     { 
      myObjectCollection.Add(new MyObject("One")); 
      myObjectCollection.Add(new MyObject("Two")); 
      return myObjectCollection; 
     } 
    } 

} 

public class MyObject : INotifyPropertyChanged 
{ 
    private string _name; 

    public MyObject(string name) 
    { 
     // TODO: Complete member initialization 
     this._name = name; 
    } 

    public string Name 
    { 
     get 
     { 
      return _name; 
     } 
    } 

    string selectedType = string.Empty; 
    public string SelectedType 
    { 
     get 
     { 
      return selectedType; 
     } 
     set 
     { 
      selectedType = value; 
      this.NotifyChange("SelectedType"); 
     } 
    } 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyChange(params object[] properties) 
    { 
     if (PropertyChanged != null) 
     { 
      foreach (string p in properties) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(p)); 
      } 
     } 
    } 
    #endregion 
} 

我會很高興,如果有人可以幫我破解這個..

感謝 拉姆

回答

5

我我不確定我是否誤解了你的問題。我認爲你的問題是關於參考問題。我稍微改了一下你的代碼,點擊按鈕後它就起作用了。

請參閱下面的代碼。

XAML:

<ComboBox Margin="0,3" 
    x:Name="MyTypeComboBox" 
    ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ListBox},Path=DataContext.Sources}" 
    SelectedValue="{Binding Path=SelectedType, Mode=TwoWay}"> 
</ComboBox> 

C#代碼:

private Collection<string> sources = new Collection<string>() { "ABC", "DEF", "XYZ" }; 
public Collection<string> Sources { get { return sources; } } 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    myObjectCollection[0].SelectedType = Sources[0]; 
    myObjectCollection[1].SelectedType = Sources[2]; 
} 
+1

非常感謝Howard ..現在工作正常!顯然,我一直在試圖設置組合框的值,但沒有綁定它。現在,當我指定(如你所建議的)一個綁定到組合框的集合時,它就像魅力一樣工作!我對嗎?數據綁定到使用ItemSource的組合框是關鍵嗎?非常感謝! – Ram 2011-04-18 16:19:58

+0

:)玩得開心。歡迎您 – Howard 2011-04-18 16:23:31

0

如何

foreach (ComboBox c in OptionsListView.Items) 
      { 
       c.SelectedValue = "Put your value here"; 
      } 

這應該做的工作,如果你有比組合框裏面可以添加其他對象

if (c is ComboBox) 

確保您正在處理正確的物體

+0

大馬士革嗨,我想你的建議正在起作用。我的listview綁定到MyObject的集合,因此foreach在OptionsListView.Items中看不到組合框。它只能看到MyObject類型的項目,這正是我的問題。幸運的是,霍華德的建議有幫助!無論如何,非常感謝在這個問題上的快速思考。 – Ram 2011-04-18 16:27:30

+1

對我來說,這似乎並不奇怪。 'ListView.Item'爲您提供一個'ItemCollection',其中包含列表中顯示的所有可視控件(例如,組合框),如果您希望帶有MyObjects的列表,則應該使用屬性ItemsSource。無論如何,希望我能幫上忙 – Damascus 2011-04-19 07:04:23