WPF: Binding z dwóch różnych klas

0

Część,
jak powiązać dane z dwóch różnych klas w pliku .xaml?

W pliku .cs mam klasę główną (MainWindow) oraz moją klasę Klasa_2:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    Klasa_2 ob = new Klasa_2("x");
    public MainWindow()
    {
        InitializeComponent();
    }

    private string name_1;

    public string Name_1
    {
        get { return name_1; }
    }

    private void Label_1_MouseDown(object sender, MouseButtonEventArgs e)
    {
        Label l = e.Source as Label;
        if (l != null)
        {
            name_1 = "abc";
            RaisePropertyChanged("Name_1");
        }
    }

   private void Label_2_MouseDown(object sender, MouseButtonEventArgs e)
   {
        ob.MouseDown(e);
   }

   public event PropertyChangedEventHandler PropertyChanged;

   private void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

public class Klasa_2 : INotifyPropertyChanged
{
    private string name_2;

    public string Name_2
    {
        get { return name_2; }
        set { name_2 = value; }
    }

    public Klasa_2(string name)
    {
        Name_2 = name_2;
    }

    public void MouseDown(MouseButtonEventArgs e)
    {
        Label l = e.Source as Label;
        if (l != null)
        {
            name_2 = "xyz";
            RaisePropertyChanged("Name_2");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
}

I teraz chciałbym w XAML powiązać właściwości z tych dwóch klas, mniej więcej tak:

<Canvas>
        <Label Content="{Binding Source=MainWindow.Name_1}" MouseDown="Label_1_MouseDown"/>
        <Label Content="{Binding Source=Klasa_2.Name_2}" MouseDown="Label_2_MouseDown" Canvas.Top="50"/>
</Canvas>

Lecz taki sposób nie działa. Czy takie wiązanie jest w ogóle możliwe?

0

Jaki wyjątek Ci wyrzuca?

W każdym razie, bez przeglądania kodu, pierwsze co mi przychodzi na myśl to utworzenie nowego obiektu w code behind, który by zawierał Name_1 i Name_2 LUB utworzenie dwóch własności które by się aktualizowały dla Name_1 i Name_2.

0

Może zrób klasę która dziedziczy po obu tych klasach i jej użyj...

0
> public partial class MainWindow : Window, INotifyPropertyChanged
> {
>     Klasa_2 ob = new Klasa_2("x");
>     public MainWindow()
>     {
>         InitializeComponent();
DataContext = this;
>     }

public Klasa_2 Ob
{
get { return ob; }
}

> 
>     private string name_1;
> 
>     public string Name_1
>     {
>         get { return name_1; }
set { name_1 = value; RaisePropertyChanged("Name_1"); }
>     }
> 
>     private void Label_1_MouseDown(object sender, MouseButtonEventArgs e)
>     {
>         Label l = e.Source as Label;
>         if (l != null)
>         {
             Name_1 = "abc";
//            RaisePropertyChanged("Name_1");
>         }
>     }
> 
>    private void Label_2_MouseDown(object sender, MouseButtonEventArgs e)
>    {
//         ob.MouseDown(e);
Ob.Name = "xyz";
>    }
> 
>    public event PropertyChangedEventHandler PropertyChanged;
> 
>    private void RaisePropertyChanged(string prop)
>     {
>         if (PropertyChanged != null)
>             PropertyChanged(this, new PropertyChangedEventArgs(prop));
>     }
> }
> 
> public class Klasa_2 : INotifyPropertyChanged
> {
>     private string name_2;
> 
>     public string Name_2
>     {
>         get { return name_2; }
>         set { name_2 = value; RaisePropertyChanged("Name_2"); }
>     }
> 
>     public Klasa_2(string name)
>     {
>         Name_2 = name_2;
>     }
> 
//     public void MouseDown(MouseButtonEventArgs e)
//     {
//         Label l = e.Source as Label;
//         if (l != null)
//         {
//             name_2 = "xyz";
//             RaisePropertyChanged("Name_2");
//         }
//     }
> 
>     public event PropertyChangedEventHandler PropertyChanged;
> 
>     public void RaisePropertyChanged(string prop)
>     {
>         if (PropertyChanged != null)
>             PropertyChanged(this, new PropertyChangedEventArgs(prop));
>     }
> }
<Canvas>
     <Label Content="{Binding Name_1, UpdateSourceTrigger=PropertyChanged}" MouseDown="Label_1_MouseDown"/>
    <Label Content="{Binding Ob.Name_2, UpdateSourceTrigger=PropertyChanged}" MouseDown="Label_2_MouseDown" Canvas.Top="50"/>
</canvas> ```
1

Zrób sobie jakiś ViewModel, który będzie zawierał wszystkie interesujące cię dane i binduj do właściwości z tego ViewModelu

2

A jaki tego sens? ViewModel to most między widokiem a modelem. Do tego właśnie służy ViewModel żeby połączyć, agregować i transformować dane z różnych modeli pod widok. Tu wygląda jakbyś używał ViewModeli jako Modeli albo odwrotnie

1 użytkowników online, w tym zalogowanych: 0, gości: 1