2013-05-28 140 views
1

我有一個名爲Channel的類,它具有以下屬性:channelName(對此問題更多但不相關)和兩個List<double>xValues,yValues)。從剛剛創建的類的綁定

public class Channel 
    { 
     public string channelName; 
     public List<double> xValues= new List<double>(); 
     public List<double> yValues= new List<double>(); 

    } 

我也有一類名爲文件,有屬性:fileNameObservableCollection<Channel>listOfChannels。文件有一個名爲read()的方法;它創建用於讀取數據的類Channel的內部對象,這取決於數據會存在可變數量的通道,並將數據存儲在列表xValuesyValues中。

public class File 
    { 
     public string fileName{ get; set; } 
     public ObservableCollection<Canal> channels{ get; set; } 
     public void read() {//stuff} 
    } 

在我的計劃,我創建了綁定到數據這樣一個ComboBox

<ComboBox x:Name="comboFile" 
        ItemsSource="{Binding myListOfChannels}" 
        DisplayMemberPath="channelName" 
        SelectedValuePath="channelName" 
        SelectedItem="{Binding selectedChannel}" /> 

myListOfChannelsselectedChannel的定義:

 public ObservableCollection<Canal> myListOfChannels { get; set; } 
     public Canal selectedChannel { get; set; } 

我他們實例稍後在代碼中正確使用。

當我點擊一個按鈕時,文件加載並創建一個新類File的對象。這是我的exampleFile

private void openButton_Click(object sender, RoutedEventArgs e) 
     { 
      File exampleFile= new File(); 
      Channel exampleChannel= new Channel(); 

      exampleFile.fileName= @"C:\Users\Path\myFile.txt"; //I haven't created OpenDialog yet 
      exampleFile.read(); 

      myListOfChannels = new ObservableCollection<Channel>(); 

      foreach (Channel mychannel in exampleFile.channels) 
      { 
       myListOfChannels .Add(mychannel); 
      } 

      selectedChannel = exampleFile.channels[0]; 
      comboFile.DataContext = this; 

     } 

這是從其他語言的翻譯,語法可能會有輕微的錯誤,但它的工作原理。 請不要完全重新設計這個,還有其他的限制。 我的問題是關於是否有可能去除多餘的分配(myListOfChannelsselectedChannel,該foreach環等),直接從我剛創建exampleFile綁定數據,是這樣的:

<ComboBox x:Name="comboFile" 
         ItemsSource="{Binding exampleFile.channels}" 
         DisplayMemberPath="exampleChannel.channelName" 
         SelectedValuePath="exampleChannel.channelName" 
         SelectedItem="{Binding selectedChannel}" /> 

我非常新手在這裏,所以如果你真的可以幫助我的寫作會很棒。我已經閱讀了幾個數據綁定教程,但我無法弄清楚這一點。

順便說一句。這可能很重要:所有這些代碼都在UserControl之內。在我的MainWindow.xaml只是UserControl的一個實例。

我已經盡力解釋我想要什麼,但是如果有什麼不清楚的地方就問一下。謝謝。

回答

1

當您在綁定上使用Path(例如{Binding Something}等效於{Binding Path=Something})或例如DisplayMemberPath,它必須引用一個屬性。不是字段(例如public string Something;)或局部變量(例如void SomeMethod() { string something; }),而是公共屬性(例如public string Something { get; set; })。

在您的代碼中,據我所知,exampleFileexampleChannel是局部變量。另外,exampleChannel似乎不被使用。你可以用這樣的東西來解決它:

public File ExampleFile { get; set; } 

private void openButton_Click(object sender, RoutedEventArgs e) 
     { 
      ExampleFile = new File(); 

      ExampleFile.fileName= @"C:\Users\Path\myFile.txt"; //I haven't created OpenDialog yet 
      ExampleFile.read(); 

      myListOfChannels = new ObservableCollection<Channel>(); 

      foreach (Channel mychannel in ExampleFile.channels) 
      { 
       myListOfChannels.Add(mychannel); 
      } 

      selectedChannel = ExampleFile.channels[0]; 
      comboFile.DataContext = this; 

     } 

(作爲一個約定,屬性中。NET USE PascalCase,所以它的ExampleFile,不exampleFile

另外值得注意的是:如果該屬性可以改變,並且要結合這種情況發生時,自動更新,你應該實現的類(ES)你INotifyPropertyChanged綁定到(在這種情況下,看起來像File,ChannelMainWindow)。

+0

謝謝!我可以刪除那個foreach循環並直接綁定ExampleFile。有可能綁定另一個UserControl的東西?例如,我有myControl1和myControl2。可以選中1中的複選框,具體取決於是否在2中選中了其他chechbox? (有點偏離主題,抱歉) – Sturm