2011-05-10 143 views
0

變化我有下面的類爲主要窗口:WPF綁定的綁定源

public partial class Window1 : Window 
{ 
    public Network network { get; set; } 
    public DataSet data_set { get; set; } 
    public Training training { get; set; } 

    public string CurrentFile { get; set; } 

    public MGTester tester; 
    public MCTester testerSSE; 

    public ChartWindow ErrorWindow; 
    public ChartWindow TimesWindow; 

    public Window1() 
    { 
     network = new Network(); 
     data_set = new DataSet(); 
     training = new Training(); 
     tester = new MGTester(); 
     testerSSE = new MCTester(); 
     CurrentFile = ""; 
     ErrorWindow = new ChartWindow(); 
     TimesWindow = new ChartWindow(); 

     InitializeComponent(); 

     for (int i = 0; i < tester.GetDeviceCount(); ++i) 
     { 
      DeviceComboBox.Items.Add(tester.GetDeviceName(i)); 
     } 
    }... 

而在我的XAML代碼我有:

<ListView Grid.Row="0" x:Name="NetworkListview" ItemsSource="{Binding network.Layers}" IsSynchronizedWithCurrentItem="True"> 
        <ListView.View> 
         <GridView> 
          <GridViewColumn Width="100" Header="layer name" DisplayMemberBinding="{Binding Name}"/> 
          <GridViewColumn Width="60" Header="neurons" CellTemplate="{StaticResource NeuronsTemplate}"/> 
          <GridViewColumn Width="110" Header="activation" CellTemplate="{StaticResource ActivationTemplate}"/> 
         </GridView> 
        </ListView.View> 
       </ListView> 

反正我綁定到窗口1的一些成員。 .. 它工作正常,但我想更改控制綁定到的成員 - 我的意思是我想在Window1中做類似的事情

this.network = new Network(); 

當我做這個綁定停止工作 - 我如何輕鬆,很好地只是「刷新」綁定?

回答

2

如果您的綁定源不是UI類,請使用通知屬性而不是自動綁定源。

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

public class MyClass : INotifyPropertyChanged 
{ 
    private Network _network; 

    public Network Network 
    { 
     get 
     { 
      return _network; 
     } 
     set 
     { 
      if (value != _network) 
      { 
       _network = value; 
       NotifyPropertyChanged(value); 
      } 
     } 
    } 


    protected NotifyPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

} 

如果源類是一個UI級,定義網絡是一個DependencyProperty:

http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx

實施例從上面引述的鏈接:

 
public class MyStateControl : ButtonBase 
{ 
    public MyStateControl() : base() { } 
    public Boolean State 
    { 
    get { return (Boolean)this.GetValue(StateProperty); } 
    set { this.SetValue(StateProperty, value); } 
    } 
    public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
    "State", typeof(Boolean), typeof(MyStateControl),new PropertyMetadata(false)); 
}