wpf绑定更改绑定源

时间:2011-05-10 20:55:05

标签: c# wpf binding

我有以下课程作为主窗口:

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>

无论如何我绑定了Window1的某个成员...... 它工作正常,但我想更改控件绑定到的成员 - 我的意思是我想在Window1

中做类似的事情
this.network = new Network();

当我这样做时,绑定停止工作 - 我怎样才能轻松而恰当地“刷新”绑定?

2 个答案:

答案 0 :(得分: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类,请将Network定义为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));
}

答案 1 :(得分:0)

有一种方法可以刷新绑定,但大多数情况下,有更好的方法:将属性设置为dependency property,或将数据放入另一个实现{{3}的类中接口。