2013-05-28 145 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