Here is some code:
MainWindow.cs:
namespace WpfApplicationProperties
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// This is a calculated property (in the real word a complex calculation...)
/// </summary>
public int MyCalculatedProperty
{
get
{
return (5 * 5);
}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
}
}
and this is the usercontrol-code (UserControl1.cs):
namespace WpfApplicationProperties
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private int _test2;
/// <summary>
/// This has to be the calculated result from the main-property
/// </summary>
public int MyProperty2
{
get
{
return _test2; // return MyCalculatedProperty; ???
}
}
private void what_Click(object sender, RoutedEventArgs e)
{
lab.Content = MyProperty2;
}
}
}
MainWindow.xaml:
<Window x:Class="WpfApplicationProperties.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:locals="clr-namespace:WpfApplicationProperties"
Title="MainWindow" Height="350" Width="525" WindowStartupLocation="CenterScreen">
<StackPanel Orientation="Vertical">
<locals:UserControl1 Height="100" Width="300" x:Name="uc"/>
<TextBox Text="{Binding MyCalculatedProperty, Mode=OneWay}" />
</StackPanel>
</Window>
and
UserControl1.xaml:
<UserControl x:Class="WpfApplicationProperties.UserControl1"
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"
mc:Ignorable="d"
d:DesignHeight="50" d:DesignWidth="200" Background="Silver">
<StackPanel Orientation="Vertical">
<Button x:Name="what" Height="20" Content="What's my property?" Click="what_Click"/>
<Label x:Name="lab" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
</StackPanel>
</UserControl>
I want the property of the usercontrol (MyProperty2) bind to the property of the MainWindow (MyCalculatedProperty), so when I need the property in the usercontrol, the value of the calculated property from the mainwindow will returned.
I tried the INotifyPropertyChanged, but it did not working.
Please, can anyone help me???