Проблема связывания WPF - PullRequest
0 голосов
/ 01 июня 2010

У меня есть некоторые привязки в пользовательском интерфейсе:

   <Window x:Class="Tester.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="377" Width="562" xmlns:my="clr-namespace:MyApp">
        <Grid>
        <TextBlock Text="{Binding Path=current.Text}"  Name="Text1" />
        <TextBlock Text="{Binding Path=current.o.Text}"  Name="Text2"  />

</Grid>
</Window>

Код:

class Coordinator : INotifyPropertyChanged{
     List<Myclass1> list;
     int currId = 0;
     public Myclass1 current{
              return list[currId];
           }
     public int CurrId
    {
        get { return currId; }
        set
        {
                currId = value;
                this.PropertyChanged(this,new PropertyChangedEventArgs("current"));


         }
}
class Myclass1{
     public string Text{get;}
     public Myclass2 o{get;}
}

class Myclass2{
     public string Text{get;}
}

Когда currId меняет Tex1 в пользовательском интерфейсе тоже меняется, но Text2 нет.

Я предполагаю, что это происходит, потому что источник Text2 не обновляется. Кто-нибудь знает как это исправить?

Ответы [ 3 ]

0 голосов
/ 01 июня 2010

Это должно работать, если вы правильно реализуете свои классы. С некоторыми исправлениями мой код выглядит так:

<Window x:Class="WpfBindingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
    <StackPanel>
        <TextBlock Text="Enter 0, 1 or 2 below and press Tab:"/>
        <TextBox Text="{Binding CurrId}"/>
        <Button/> <!--Button here is just for another tab stop-->
        <TextBlock Text="{Binding Path=current.Text}"  Name="Text1" />
        <TextBlock Text="{Binding Path=current.o.Text}"  Name="Text2"  />
    </StackPanel>
</Grid>
</Window>

отлично работает с классами, реализованными так:

public partial class Window1: Window {
    public Window1() {
        InitializeComponent();
        DataContext = new Coordinator();
    }
}

class Coordinator: INotifyPropertyChanged {
    List<Myclass1> list;
    int currId = 0;

    public Myclass1 current {
        get { return list[currId]; }
    }

    public int CurrId {
        get { return currId; }
        set {
            currId = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("current"));
        }
    }

    public Coordinator() {
        list = new List<Myclass1>(){
            new Myclass1(){ Text = "1", o = new Myclass2(){Text="1.1"}},
            new Myclass1(){ Text = "2", o = new Myclass2(){Text="2.2"}},
            new Myclass1(){ Text = "3", o = new Myclass2(){Text="3.3"}}
        };
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

class Myclass1 {
    public string Text { get; set; }
    public Myclass2 o { get; set; }
}

class Myclass2 {
    public string Text { get; set; }
}
0 голосов
/ 02 июня 2010

Может быть, вам также нужно реализовать INotifyPropertyChanged в Myclass2.

0 голосов
/ 01 июня 2010

Я не уверен, что это то, что вы ищете, но для чего-то подобного я использовал PropertyObserver класс из MVSM фреймворка Джоша Смита .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...