Jak zrobisz to po ludzku to nie będziesz miał problemów:
Zobacz ten przykład:
UserControl:
public partial class UC : UserControl
{
public UC()
{
InitializeComponent();
NewPerson = new Person("Kowalski");
OldPerson = new Person("Kowalski");
}
public string Surname
{
get { return (string)GetValue(SurnameProperty); }
set { SetValue(SurnameProperty, value); }
}
public static readonly DependencyProperty SurnameProperty =
DependencyProperty.Register("Surname", typeof(string), typeof(UC), new PropertyMetadata(string.Empty, (DependencyObject d, DependencyPropertyChangedEventArgs e) => {
(d as UC).SurnameChanged((e.NewValue == null) ? string.Empty : e.NewValue.ToString());
}));
internal void SurnameChanged(string NewSurname)
{
Dispatcher?.Invoke(() =>
{
OldPerson = NewPerson;
NewPerson = new Person(NewSurname);
});
}
public Person NewPerson
{
get { return (Person)GetValue(NewPersonProperty); }
set { SetValue(NewPersonProperty, value); }
}
public static readonly DependencyProperty NewPersonProperty =
DependencyProperty.Register("NewPerson", typeof(Person), typeof(UC), new PropertyMetadata(null));
public Person OldPerson
{
get { return (Person)GetValue(OldPersonProperty); }
set { SetValue(OldPersonProperty, value); }
}
public static readonly DependencyProperty OldPersonProperty =
DependencyProperty.Register("OldPerson", typeof(Person), typeof(UC), new PropertyMetadata(null));
public class Person : NotifyPropertyChanged, INotifyPropertyChanged
{
public Person(string Surname)
{
this.Surname = Surname;
}
private string surname;
public int Surname { get { return surname; } set { surname = value; PropertyChanged("Surname"); } }
}
}
UserControl x:Class="WpfApp.UC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp"
x:Name="hWindow"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<TextBox Text="{Binding ElementName=hWindow, Path=Surname, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
</Grid>
</UserControl>
MainWindow:
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<local:UC x:Name="hUC"/>
</Grid>
</Window>
namespace WpfApp
{
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Person Actual = hUC.NewPerson;
Person Old = hUC.OldPerson;
}
}
}
To i tak nie jest do końca po bożemu ale lepiej niż ustalanie DataContext w kodzie
EDIT:
Założyciel tematu napisał, że nie wyjaśniłem nic.
Dlatego na starcie poczytaj o:
- Oddzieleniu warstwy widoku od warstwy logicznej - To na całkowity start.
- Pakuj ile się da do XAMLA. Jeżeli dasz radę bindować nie przypisuj zmiennej w warstwie logicznej do widoku. jeżeli zmienne mają różny typ używaj konwerterów.
- Poczytaj o wcześniej wspomnianym INotifyPropertyChanged oraz DependencyProperty
- A to najważniejsze:
jeżeli chcesz dobrej odpowiedzi w swoim wątku nie wstawiaj pytania typu:
Dlaczego metoda Start() zwraca null?
public object Start
{
//Tutaj jest kod ale nie chce mi sie wklejać całego projektu
}
Musisz podać więcej informacji. Akurat jeżeli chodzi o Twój wątek bardzo ważne jest co się dzieje w warstwie widoku, więc wstaw również kod XAML.. Jeżeli kod jest bardzo duży wstaw go na pastebin.com